SQL where 子句中使用的计数
SQL count used in where clause
我是 sql 的新手。我有一个问题涉及使用函数 count() 作为 sql.
中 where-clause 的限制
tbl_meeting
ID
TIMESTAMP
CAPACITY
...
tbl_attendance
ID
NAME
...
(我也有一个table,因为多人出席可以参加多个会议)
我要select所有的会议都坐满了。它应该看起来像这样,但我可以弄明白。
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
谢谢。
试试这个:
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
您已经在 select 语句中执行了计数函数,因此您的 where 子句只需要与数字进行比较。
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)
使用此方法查找会议的全部出席人数
SELECT *
FROM tbl_meetings M
WHERE M.capacity = (SELECT COUNT(LAM.id)
FROM tbl_lnk_attendance_meeting LAM
WHERE M.id = LAM.meeting_id)
我是 sql 的新手。我有一个问题涉及使用函数 count() 作为 sql.
中 where-clause 的限制tbl_meeting
ID
TIMESTAMP
CAPACITY
...
tbl_attendance
ID
NAME
...
(我也有一个table,因为多人出席可以参加多个会议)
我要select所有的会议都坐满了。它应该看起来像这样,但我可以弄明白。
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
谢谢。
试试这个:
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
您已经在 select 语句中执行了计数函数,因此您的 where 子句只需要与数字进行比较。
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)
使用此方法查找会议的全部出席人数
SELECT *
FROM tbl_meetings M
WHERE M.capacity = (SELECT COUNT(LAM.id)
FROM tbl_lnk_attendance_meeting LAM
WHERE M.id = LAM.meeting_id)