尝试仅选择是否符合条件

时间:2019-07-23 15:01:18

标签: mysql if-statement nested

我正在尝试缩小搜索范围并将其分组。下面是我的查询。它将所有签到的与会者指定为特定的会话名称。

这些是现在的结果

-------|---|------------------|----------------|----|
Person1 One SKA - Monday Aft    SK Team          1
Person1 One SKA - Monday Eve    Commander Dana   1
Person1 One SKA - Monday Morn   Commander Linda  1
Person2 Two SKA - Monday Aft    SK Team          1
Person2 Two SKA - Monday Morn   Commander Linda  1

我想做的是缩小结果范围,以使只有参加所有三个会议的人才能参加会议?例如,在这种情况下,人员1参加了所有三个人,但没有参加人员2。

SELECT ea.first_name, ea.last_name, es.name, es.keynote_speaker,
        count(esc.id = ea.id  ) as Times_Checked_In
FROM event e
JOIN  event_session es ON e.id =  es.event_id
JOIN event_session_checkin esc on es.id = esc.event_session_id
JOIN event_attendee ea ON esc.event_attendee_id = ea.id
WHERE  e.external_id='2019SWBC' and es.name like 'SKA - M%'
group by ea.id, es.name,es.keynote_speaker, ea.first_name, 
ea.last_name
order by ea.id desc;

任何可以帮助我的忍者Mysql专家

还有,我该如何编辑列1,而不是显示#1,而是显示默认的字符串“是”?这样我就可以将该列命名为“全部参加”?然后说“是”

1 个答案:

答案 0 :(得分:0)

您非常接近,只需在组后添加HAVING子句即可进行过滤:

SELECT ea.first_name, ea.last_name, es.name, es.keynote_speaker,
        count(*) as Times_Checked_In
FROM event e
JOIN  event_session es ON e.id =  es.event_id
JOIN event_session_checkin esc on es.id = esc.event_session_id
JOIN event_attendee ea ON esc.event_attendee_id = ea.id
WHERE  e.external_id='2019SWBC' and es.name like 'SKA - M%'
group by ea.id, es.name,es.keynote_speaker, ea.first_name, 
ea.last_name
HAVING COUNT(*)=3
order by ea.id desc;