MySQL中條件放在where後面與放在on後面的區別
阿新 • • 發佈:2018-08-24
ESS position 運行 image info test mys 假設 mysql
假設有兩種表:test_on_position表和address表,address表存放地址,test_on_position存放會員及其常用的地址,數據如下:
address表:
test_on_position表:
1. left join
條件在where後面
SELECT
*
FROM
test_on_position
LEFT JOIN address ON test_on_position.address = address.id
WHERE
test_on_position.address IS NOT NULL
運行結果:
條件在on後面
SELECT * FROM test_on_position LEFT JOIN address ON test_on_position.address = address.id AND test_on_position.address IS NOT NULL
運行結果:
結果分析
當條件放在on後面時,無論on條件的真假,都會返回左表的值;
當條件放在where後面時,只有滿足條件的記錄會返回。
inner join
條件在on後面
SELECT
*
FROM
test_on_position
INNER JOIN address ON test_on_position.address = address.id AND test_on_position.address IS NOT NULL
運行結果:
條件在where後面
SELECT * FROM test_on_position INNER JOIN address ON test_on_position.address = address.id WHERE test_on_position.address IS NOT NULL
運行結果:
結果分析
無論條件放在哪裏,都只會返回滿足條件的結果。
MySQL中條件放在where後面與放在on後面的區別