SQL学习笔记——MySQL求差集

一些数据库,如SQL server、Oracle可以使用except,

但是MySQL还不支持except语句,目前有三种替代方法:

(1)外连接

(2)not exists

(3)not in

 

select a.id,a.name

from class_a a left join class_b b on a.id=b.id

where b.id is null;

 

select * from class_a a

where not exists

(select * from class_b b where a.id=b.id);

 

select * from class_a a

where a.id not in

(select b.id from class_b b where a.id=b.id);

 

外连接的效率更高

 

【注】

思路来自于网络与MICK的两本SQL教程,分别是《SQL基础教程》与《SQL进阶教程》。这两本书内容深入浅出,非常值得学习。

你可能感兴趣的:(SQL)