数据库查询存在一个表不在另外一个表的数据

  1. 子查询,条件用not int (所有数据库都可使用

select distinct A.ID from A where A.ID not in (select ID from B)

  1. 关联查询 使用 left join...on... , "B.ID isnull" 表示左连接之后在B.ID 字段为 null的记录

select A.ID from A left join B on A.ID=B.ID where B.ID is null

  1. 子查询,统计作为条件判断(mysql可以使用,clickhouse不适用

select * from B where (select count(1) as num from A where A.ID = B.ID) = 0

posted @ 2022-04-25 13:57  梦山南  阅读(334)  评论(0)    收藏  举报