Mysql Not in有null值查詢的問題

來源:互聯網
上載者:User

標籤:

今天發現Mysql的not in使用的一個問題,大致是:

select * from A where id not in (select fid from B).

發現查詢結果無論如何都是0條記錄。後來發現B裡面返回的查詢結果集有一條NULL值,查了資料才知道mysql 的not in裡面如果有一個NULL值,將返回0條記錄。

要解決這個問題需要把 select fid from B 變成

select fid from B where B.fid is not null

這個問題在其他的資料庫裡也會有,是因為 not in的處理有問題。再具體詳細說明一下,如下:

select * from A where id not in (1,2,null )

那麼就沒有記錄返回,因為裡面有null值,當用id和null比較的時候,會返回false,所以一條記錄都不會返回,所以要改成下面就可以了:

select * from A where id not in (select bid from B where bid  is not null ) 

Mysql Not in有null值查詢的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.