当前位置:Gxlcms > 数据库问题 > MySQL中条件放在where后面与放在on后面的区别

MySQL中条件放在where后面与放在on后面的区别

时间:2021-07-01 10:21:17 帮助过:50人阅读

* 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后面的区别

标签:left join   ddr   结果   address   分析   inf   res   sel   osi   

人气教程排行