解決mysql left join 無法顯示左表的所有數據


一、問題:查詢結果無法顯示test1 的全部數據

SELECT *
FROM test1 a
left join test2 b on b.name=a.name
where b.createtimelike '%2021-03-09 00:00:00%'
and b.post='Java工程師'

二、解決方案

1、取消where條件,將where改為and即可實現

SELECT *
FROM test1 a
left join test2 b on b.name=a.name
and b.createtimelike '%2021-03-09 00:00:00%'
and b.post='Java工程師'

注意:以上篩選條件都是從b表也就是附表中

2、 如果要篩選主表也就是左表的數據,則需將主表的條件放置在where

SELECT *
FROM test1 a
left join test2 b on b.name=a.name
and b.createtimelike '%2021-03-09 00:00:00%'
and b.post='Java工程師'
where available = 1

注:join on 后邊的條件只針對附表
如果要篩選主表,必須寫在where后

轉自:https://blog.csdn.net/weixin_44195615/article/details/114683063


免責聲明!

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



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