CROSS JOIN 是不带 ON 子句的 INNER JOIN 的同义词吗?

发布于 2024-11-02 19:37:02 字数 403 浏览 0 评论 0原文

我想知道在任何查询中找到 CROSS JOIN 是否可以安全地替换为 INNER JOIN

没有 ONUSINGINNER JOINCROSS JOIN 完全相同吗?如果是,那么 CROSS JOIN 类型的发明仅仅是为了在查询中更好地表达意图吗?

这个问题的附录是:

在使用CROSS JOIN ... WHERE xINNER 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

开始看清了 2024-11-09 19:37:02

在所有现代数据库中,所有这些构造都针对相同的计划进行了优化。

某些数据库(例如 SQL Server)需要在 INNER JOIN 之后有一个 ON 条件,因此您的第三个查询不会在那里进行解析。

表的可见性范围按 JOIN 顺序排列,因此此查询:

SELECT  *
FROM    s1
JOIN    s2
ON      s1.id IN (s2.id, s3.id)
CROSS JOIN
        s3

不会解析,而此查询:

SELECT  *
FROM    s2
CROSS JOIN
        s3
JOIN    s1
ON      s1.id IN (s2.id, s3.id)

会解析。

In all modern databases all these constructs are optimized to the same plan.

Some databases (like SQL Server) require an ON condition after the INNER JOIN, so your third query just won't parse there.

Visibility scope of the tables is in the JOIN order, so this query:

SELECT  *
FROM    s1
JOIN    s2
ON      s1.id IN (s2.id, s3.id)
CROSS JOIN
        s3

won't parse, while this one:

SELECT  *
FROM    s2
CROSS JOIN
        s3
JOIN    s1
ON      s1.id IN (s2.id, s3.id)

will.

苍风燃霜 2024-11-09 19:37:02

原始交叉联接是一种没有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文