这两种写法是等价的吗
select a.*, b.* from A a left join B b where a.bid = b.id
select a.*, b.* from B b right join A a where a.bid = b.id
感谢大家的回复!
更正一下主题里面on笔误写成了where @hemingyang .
昨晚翻看了《数据库原理与应用 - 基于SQL Server》 它里面是这样举例子的: 如果把两种表当作两个集合,那么
inner join就是两个集合的交集left join 就是交集 + 左集合right join 就是交集 + 右集合这样的话,参考stackoverflow上的讨论,
将left join 换成 right join,再将A B 表交换位置,查出的结果是等价的。
1
saulshao Jul 16, 2020
|
2
saulshao Jul 16, 2020
如果你的 a,b 两个表中的 id 都是唯一索引,等价这个说法是成立的,但是假如 a 表中的 id 对应的行比 b 表多,我记得左连接结果会以 a 表中的行为准,反之也成立
|
3
EminemW Jul 16, 2020
*** a left join b ****会保留 a 表存在 但 b 表不存在的数据
|
4
asLw0P981N0M0TCC Jul 16, 2020
新建 2 个表执行下?
|
6
no1xsyzy Jul 17, 2020
输出结果是一样的
但中间的运行步骤是否一样就不清楚了。直觉上应该不会有不同。具体要 explain,微弱可能依赖于特定实现。 |
7
leoleoasd Jul 17, 2020
|
8
leoleoasd Jul 17, 2020
看上去, 除了语义, 没啥不同?
|
9
leoleoasd Jul 17, 2020
"There are no performance that can be gained if you'll rearrange LEFT JOINs to RIGHT."
|
10
zhangzhiming Jul 17, 2020
不是 用 on 吗 为啥 你写 where
|
11
liujavamail Jul 17, 2020
所以 left join 或者 right join 只是查询数据范围大小不一样,但加了 where 的条件之后, 最后都是一样的结果, 可以这么理解吗?
就是如果 左表数据少,就用 left join, 右表数据少,就用 right join |
12
saulshao Jul 17, 2020
我以前似乎是左表数据多采用 left join.....反之用 right join...
但是假如你是真的要求交集,#11 的说法是正确的 |
13
zhangysh1995 Jul 19, 2020
@git00ll
一句回答:A LEFT JOIN B 和 B RIGHT JOIN A 是等价的。LEFT JOIN 保留左表所有行,RIGHT JOIN 保留右边所有行。 我自己刚写过一篇理解 JOIN 的文章,欢迎赏个脸 https://zhuanlan.zhihu.com/p/157249501 @hemingyang WHERE 和 ON 在 JOIN 情况下没有什么区别。SQL Server 不太了解,在 MySQL 里面,ON 部分的条件也可以写 WHERE 里面,但是习惯是 ON 写 JOIN 条件,WHERE 写结果条件。文档如下: The search_condition used with ON is any conditional expression of the form that can be used in a WHERE clause. Generally, the ON clause serves for conditions that specify how to join tables, and the WHERE clause restricts which rows to include in the result set. |
14
JasonLaw Jul 19, 2020
@zhangysh1995 #13
你说“WHERE 和 ON 在 JOIN 情况下没有什么区别”,你是说 inner join 吗?如果是的话,你说得没错,但是我想更加明确地说出,where 和 on 在 outer join 的表现是有区别的。 在“Database System Concepts 6th edition - Chapter 4 Intermediate SQL - 4.1 Join Expressions - 4.1.1 Join Conditions”中,说了“However, there are two good reasons for introducing the on condition. First, we shall see shortly that for a kind of join called an outer join, on conditions do behave in a manner different from where conditions. Second, an SQL query is often more readable by humans if the join condition is specified in the on clause and the rest of the conditions appear in the where clause.”。之后在“4.1.2 Outer Joins”中,它说“As we noted earlier, on and where behave differently for outer join. The reason for this is that outer join adds null-padded tuples only for those tuples that do not contribute to the result of the corresponding inner join. The on condition is part of the outer join specification, but a where clause is not.”。 我这里用例子演示一下 on 和 where 的区别吧。 create table t1(c1 int); create table t2(c2 int); insert into t1 values(1),(2),(3); insert into t2 values(1); select t1.*, t2.* from t1 left join t2 on t1.c1 = t2.c2; 的结果为 +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | | 2 | NULL | | 3 | NULL | +------+------+ select t1.*, t2.* from t1 left join t2 on true where t1.c1 = t2.c2; 的结果为 +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ |
15
JasonLaw Jul 19, 2020
|
16
zhangysh1995 Jul 20, 2020
@JasonLaw 我没有说 select t1.*, t2.* from t1 left join t2 on t1.c1 = t2.c2; 和 select t1.*, t2.* from t1 left join t2 on true where t1.c1 = t2.c2; 是一样的。很明显你给的两条并不是等价的。我指的是同一个条件放在 on 或者放在 where 是一样的,请不要曲解意思。
这两条是等价的。 select t1.*, t2.* from t1 left join t2 on t1.c1 = t2.c2; select t1.*, t2.* from t1 left join t2 where t1.c1 = t2.c2; |
17
JasonLaw Jul 20, 2020
@zhangysh1995 你可以自己去执行一下,看看是不是等价的,如果是的话,麻烦分享一下语句,让我重现一下。
|