SQL語句技巧:查詢存在一個表而不在另一個表中的數據記錄
http://www.cnblogs.com/zuowj/p/4013434.html
方法一(僅適用單個字段)
使用 not in ,容易理解,效率低
select A.ID from A where A.ID not in (select ID from B)
方法二(適用多個字段匹配)
使用 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
方法三(適用多個字段匹配)
select * from B where (select count(1) as num from A where A.ID = B.ID) = 0
方法四(適用多個字段匹配)
select * from A where not exists(select 1 from B where A.ID=B.ID)
附:select 1 from B這里的意思是只要B中有值就是顯示1 或者理解為:表有多少條記錄,結果就是多少條"1"的行.
就是如果表里如果有記錄,就顯示1 簡單理解就是不查詢具體列,只要有值就顯示1