gpt4 book ai didi

sql - LEFT JOIN 形式之间的差异

转载 作者:行者123 更新时间:2023-12-04 19:13:26 25 4
gpt4 key购买 nike

这些查询有什么区别

SELECT
A.AnyField
FROM
A
LEFT JOIN B ON B.AId = A.Id
LEFT JOIN C ON B.CId = C.Id

这是另一个查询

SELECT
A.AnyField
FROM
A
LEFT JOIN
(
B
JOIN C ON B.CId = C.Id
) ON B.AId = A.Id

最佳答案

原答案:

它们不一样。

例如,a left join b left join c 将返回 a 行,加上 b 行,即使没有 c 行也是如此。

a left join (b join c) 如果没有 c 行,则永远不会返回 b 行。

稍后添加:

SQL>create table a (id int); 
SQL>create table b (id int);
SQL>create table c (id int);
SQL>insert into a values (1);
SQL>insert into a values (2);
SQL>insert into b values (1);
SQL>insert into b values (1);
SQL>insert into c values (2);
SQL>insert into c values (2);
SQL>insert into c values (2);
SQL>insert into c values (2);
SQL>select a.id from a left join b on a.id = b.id left join c on b.id = c.id;
id
===========
1
1
2

3 rows found

SQL>select a.id from a left join (b join c on b.id = c.id) on a.id = b.id;
id
===========
1
2

2 rows found

关于sql - LEFT JOIN 形式之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29607795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com