更新时间:2022-06-03 18:03:34
对于这种内部联接,它们在逻辑上是等效的.但是,您可能会遇到join子句中的条件与where子句中的条件有所不同的情况.
For inner joins like this they are logically equivalent. However, you can run in to situations where a condition in the join clause means something different than a condition in the where clause.
作为一个简单的例子,想象一下您像这样进行左连接;
As a simple illustration, imagine you do a left join like so;
select x.id
from x
left join y
on x.id = y.id
;
在这里,我们要从x中获取所有行,而不管y中是否有匹配的id.现在说我们的加入条件越来越大-我们不仅在基于id的y中寻找匹配项,而且还在基于id_type的情况中寻找匹配项.
Here we're taking all the rows from x, regardless of whether there is a matching id in y. Now let's say our join condition grows - we're not just looking for matches in y based on the id but also on id_type.
select x.id
from x
left join y
on x.id = y.id
and y.id_type = 'some type'
;
同样,这将给出x中的所有行,而不管y中是否存在匹配的(id,id_type).
Again this gives all the rows in x regardless of whether there is a matching (id, id_type) in y.
这是非常不同的:
select x.id
from x
left join y
on x.id = y.id
where y.id_type = 'some type'
;
在这种情况下,我们选择x的所有行,并尝试与y的行匹配.现在,对于y中不匹配的行,y.id_type将为null.因此,y.id_type ='some type'不被满足,因此那些没有匹配项的行将被丢弃,从而有效地将其转换为内部联接.
In this situation, we're picking all the rows of x and trying to match to rows from y. Now for rows for which there is no match in y, y.id_type will be null. Because of that, y.id_type = 'some type' isn't satisfied, so those rows where there is no match are discarded, which effectively turned this in to an inner join.
长话短说:对于内部联接而言,条件在哪里都无所谓,对于外部联接而言,则可以.
Long story short: for inner joins it doesn't matter where the conditions go but for outer joins it can.