mysql exists與not exists實例詳解
tableA
|column1 | column1 |column3 |
tableb
|column1 | column1 |column3 |
要查詢 tableA 的數(shù)據(jù),條件是是 tableA.column1 不在 tableB 的 tableB.column2 中
也就是要得到類似以下語句的效果(not in 效果不完全等同于 not exists , 如果子查詢中出現(xiàn)空記錄, 則整個查詢語句不會返回數(shù)據(jù))
SELECT
a.*
FROM
tableA a
WHERE
a.column1 not in (
SELECT column2 FROM tableB
)
可以使用如下語句來實現(xiàn)
SELECT
a.*
FROM
tableA a
WHERE
NOT EXISTS(
SELECT b.column2 FROM tableB b WHERE a.colunm1=b.column2
)
以上只是兩張表的情況, 其實在多張表的連接查詢中也是比較好用的. 以上寫法同樣適用于exists
以上就是mysql exists與not exists的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:- MySQL中in與exists的使用及區(qū)別介紹
- 對比分析MySQL語句中的IN 和Exists
- MySQL exists 和in 詳解及區(qū)別
- mySQL中in查詢與exists查詢的區(qū)別小結(jié)
- MySQL關(guān)于exists的一個bug
- 安裝mysql出錯”A Windows service with the name MySQL already exists.“如何解決
- MySQL的子查詢中FROM和EXISTS子句的使用教程
- MYSQL IN 與 EXISTS 的優(yōu)化示例介紹
- mysql not in、left join、IS NULL、NOT EXISTS 效率問題記錄
- UCenter info: MySQL Query Error SQL:SELECT value FROM [Table]vars WHERE noteexists
- Mysql exists用法小結(jié)