我有两个mysql表,event_template和event_occurrence,其中event_occurrence有一个event_template id的列。我想找到所有没有event_occurrence的event_templates。
尝试此查询我得到0行:
SELECT * FROM `event_template` where id NOT IN (SELECT event_template_id FROM event_occurrence)
请注意,event_template有166行,此查询返回162行:
SELECT * FROM `event_template` WHERE id IN (SELECT event_template_id FROM event_occurrence)
答案 0 :(得分:0)
您应该使用NOT EXISTS
代替NOT IN
。这应该适合你:
SELECT *
FROM event_template et
WHERE NOT EXISTS (SELECT NULL FROM event_occurrence eo where eo.event_template_id = et.id)