我不确定如何表达这个问题,但问题就在这里。
我有一个电子邮件表 subscribers
,如下所示:
Email - Event_id
1@1.com - 123
1@1.com - 456
2@2.com - 123
3@3.com - 123
3@3.com - 123
我有一个如下所示的查询:
select email as "Email Address"
from subscribers
where event_id='123'
GROUP BY email
我希望这个查询的结果是:
Email Address
2@2.com
3@3.com
但显然基于我得到的查询:
Email Address
1@1.com
2@2.com
3@3.com
基本上我想排除与其他事件 ID 关联的电子邮件,只收集那些出现在 ID 123
最佳答案
只需使用 having
子句并将条件移到那里:
select email as "Email Address"
from subscribers
group by email
having min(event_id) = max(event_id) and min(event_id) = '123';
这表示电子邮件中的最小 event_id 与最大值相同(因此全部相等或 NULL
)并且值为 '123'
.
关于mysql - SQL 查询排除出现在其他行中的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33291859/