sql查询:存在A表而不在B表中的id

A、B两表,找出ID字段中,存在A表,但是不存在B表的ID

A表:id     B表:id

    1      1

    2      2

    3

    4

    5

#第一种:子查询
SELECT A.id from A where A.id not in (SELECT id from B);
#当B表中的id数值比较多的时候,那么A 表中的id 会匹配B表中的每个id,那么效率就会低

#第二种:join 查询
SELECT A.id FROM A LEFT JOIN B on A.id = B.id where B.id is NULL;
#join 查询,相比子查询,效率就要快的多

#第三种:逻辑相对复杂,但是速度最快
SELECT * from A where (SELECT count(1) as num from B where A.id = B.id) = 0;

 

posted @ 2020-10-21 15:11  黑桃G  阅读(1658)  评论(0)    收藏  举报