CROSS JOIN 是不带 ON 子句的 INNER JOIN 的同义词吗?
我想知道在任何查询中找到 CROSS JOIN
是否可以安全地替换为 INNER JOIN
。
没有 ON
或 USING
的 INNER JOIN
与 CROSS JOIN
完全相同吗?如果是,那么 CROSS JOIN 类型的发明仅仅是为了在查询中更好地表达意图吗?
这个问题的附录是:
在使用CROSS JOIN ... WHERE x
、INNER JOIN ... ON ( x )
时,使用现代且广泛使用的DBMS是否有区别?代码>或<代码>内部连接...哪里(x)?
谢谢。
I am wondering whether CROSS JOIN
can be safely replaced with INNER JOIN
in any query when it is found.
Is an INNER JOIN
without ON
or USING
exactly the same as CROSS JOIN
? If yes, has the CROSS JOIN
type been invented only to express intent better in a query?
An appendix to this question would be:
Can there be a difference using modern and widely used DBMSes when using CROSS JOIN ... WHERE x
, INNER JOIN ... ON ( x )
or INNER JOIN ... WHERE ( x )
?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在所有现代数据库中,所有这些构造都针对相同的计划进行了优化。
某些数据库(例如
SQL Server
)需要在INNER JOIN
之后有一个ON
条件,因此您的第三个查询不会在那里进行解析。表的可见性范围按
JOIN
顺序排列,因此此查询:不会解析,而此查询:
会解析。
In all modern databases all these constructs are optimized to the same plan.
Some databases (like
SQL Server
) require anON
condition after theINNER JOIN
, so your third query just won't parse there.Visibility scope of the tables is in the
JOIN
order, so this query:won't parse, while this one:
will.
原始交叉联接是一种没有 where 子句的联接,这意味着左表和右表的每一种组合都会生成一条记录,并在没有左侧或右侧数据的情况下插入空值。
如果将 where 子句添加到交叉连接,则它相当于内部连接,因为 where 子句与内部连接中的 ON 执行相同的操作。
然而,内部联接通常更好用,因为它将 ON 条件与其余的 where 子句分开,使其更容易理解。
A raw cross join is one that has no where clause meaning that one record is produced for every combination of the left and right tables being joined with nulls inserted where there is no left or right side data.
If you add a where clause to a cross join this makes it equivalent to an inner join as the where clause does the same thing as the ON in the inner join.
However, inner joins are generally nicer to use as this separates the ON condition away from the rest of your where clauses making it easier to understand.