關于mysql exists與not exists分析

本文主要介紹了mysql exists與not exists實例詳解的相關資料,鑒于 not exists 的效率往往要高于 not in , 所以一般情況下會使用前者替代后者,需要的朋友可以參考下,希望能幫助到大家。

mysql exists與not exists實例詳解

tableA

|column1 | column1 |column3 |

tableb

|column1 | column1 |column3 |

要查詢 tableA 的數據,條件是是 tableA.column1 不在 tableB 的 tableB.column2 中

也就是要得到類似以下語句的效果(not in 效果不完全等同于 not exists , 如果子查詢中出現空記錄, 則整個查詢語句不會返回數據)

SELECT     a.*  FROM    tableA a  WHERE     a.column1 not in (      SELECT column2 FROM tableB    )

可以使用如下語句來實現

SELECT    a.*  FROM    tableA a  WHERE    NOT EXISTS(      SELECT b.column2 FROM tableB b WHERE a.colunm1=b.column2    )

以上只是兩張表的情況, 其實在多張表的連接查詢中也是比較好用的. 以上寫法同樣適用于exists

相關推薦:

php file_exists()函數沒有效果是因為什么?

php file_exists()函數沒有效果是因為什么?

php file_exists()函數沒有效果是因為什么?

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享