sql⾼效查询两表之间不同的数据
本⽂推荐三种⽅法,第三种为最⾼效
1.使⽤not in 结合 distinctselect distinct from
SELECT DISTINCT a.id FROM a WHERE a.id NOT in(SELECT id FROM b)
2.使⽤left join做关联查询且条件为关联表的id is null
SELECT * from a LEFT JOIN b ON a.id = b.id WHERE b.id IS NULL
3.充分利⽤count函数,若⼦查询的结果不为0,则表明该Id存在1条或多条,不会查询出此条记录,最⾼效!select * from b where (select count(1) from a where a.ID = b.ID) = 0