【MySQL】查詢不在表中的數據


1、方法一(僅適用單個字段):使用 not in ,比較容易理解,缺點是效率低

如:select A.ID from A where A.ID not in (select ID from B);

2、方法二(適用多個字段匹配):使用 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 ;

3、方法三(適用多個字段匹配)

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

4、方法四(適用多個字段匹配)

如:select * from A where not exists(select 1 from B where A.ID=B.ID)

https://zhidao.baidu.com/question/1447661372863253100.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM