SQL CROSS JOIN 是没有 ON 子句的 INNER JOIN 的同义词吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5742196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:14:38  来源:igfitidea点击:

Is CROSS JOIN a synonym for INNER JOIN without ON clause?

sqlinner-joincross-join

提问by Benoit

I am wondering whether CROSS JOINcan be safely replaced with INNER JOINin any query when it is found.

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

Is an INNER JOINwithout ONor USINGexactly the same as CROSS JOIN? If yes, has the CROSS JOINtype been invented only to express intent better in a query?

INNER JOIN没有ON还是USING完全一样CROSS JOIN?如果是,该CROSS JOIN类型是否只是为了在查询中更好地表达意图而发明的?

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 )?

使用CROSS JOIN ... WHERE x, INNER JOIN ... ON ( x )或时使用现代和广泛使用的 DBMS 是否有区别INNER JOIN ... WHERE ( x )

Thank you.

谢谢你。

采纳答案by Quassnoi

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

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

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

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

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

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

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.

将要。

回答by Chris Snowden

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.

原始交叉联接是一种没有 where 子句的交叉联接,这意味着为左表和右表的每个组合生成一条记录,并在没有左侧或右侧数据的情况下插入空值。

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.

如果将 where 子句添加到交叉连接,则它等同于内部连接,因为 where 子句与内部连接中的 ON 执行相同的操作。

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.

但是,内部联接通常更易于使用,因为这将 ON 条件与其他 where 子句分开,使其更易于理解。