+ -
当前位置:首页 → 问答吧 → 列出不存在的条目

列出不存在的条目

时间:2011-07-14

来源:互联网

a 表
id name
-------------------
1 aa
2 bb
3 cc
4 dd


b 表
id a_id date
------------------
1 1 7:00
2 1 8:00
3 3 8:05


求获取没有在 B 表中出现的 A 表 id
id name
--------------------
2 bb
4 dd


作者: dgxs34   发布时间: 2011-07-14

SQL code
select  *
from a
where id not in (select a_id from b)

作者: rucypli   发布时间: 2011-07-14

SQL code
select *
from a
where not exists (select 1 from b where a.id = b.a_id)

理论上,数据多的时候,这个更好

作者: shine333   发布时间: 2011-07-14