Reputation: 229
I'm kind of new to sql. I have got a question involving using the function count() as restriction the the where-clause in my sql.
tbl_meeting
ID
TIMESTAMP
CAPACITY
...
tbl_attendance
ID
NAME
...
(I've also got a table between because multiple attendance can go to multiple meetings)
I want to select all the meeting that are full. It should look something like this but i can figure it out.
select *
, count(LAM.id) as amount_attending
from tbl_meetings M
left join tbl_lnk_attendance_meeting LAM
on M.id = LAM.meeting_id
where M.capacity <> amount_attending
Thanks.
Upvotes: 1
Views: 81
Reputation: 12309
Use this approach to find full attendance of meeting
SELECT *
FROM tbl_meetings M
WHERE M.capacity = (SELECT COUNT(LAM.id)
FROM tbl_lnk_attendance_meeting LAM
WHERE M.id = LAM.meeting_id)
Upvotes: 1
Reputation: 33
You're already carrying out the count function in your select statement so your where clause just needs a comparison to a number.
select *
, count(LAM.id) as amount_attending
from tbl_meetings M
left join tbl_lnk_attendance_meeting LAM
on M.id = LAM.meeting_id
where amount_attending = (whatever number constitutes a full meeting)
Upvotes: 0
Reputation: 2202
Try this:
select * , count(LAM.id) as amount_attending
from tbl_meetings M
left join tbl_lnk_attendance_meeting LAM
on M.id = LAM.meeting_id
group by LAM.id
Upvotes: 0