什么是默认的 MySQL JOIN 行为,INNER 还是 OUTER?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4418776/
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-08-31 17:58:43  来源:igfitidea点击:

What is the default MySQL JOIN behaviour, INNER or OUTER?

mysqlselectjoin

提问by Quang Van

So I've been looking through the internet the last hour, reading and looking for the definitive answer to this simple question.

所以我过去一个小时一直在浏览互联网,阅读并寻找这个简单问题的明确答案。

What is the default JOIN in MySQL?

MySQL 中的默认 JOIN 是什么?

SELECT * FROM t1 JOIN t2

Is that the same as

是不是一样

SELECT * FROM t1, t2

OR

SELECT * FROM t1 INNER JOIN t2

Also a related question, when you use "WHERE" clauses, is it the same as JOIN or INNER JOIN ?

还有一个相关的问题,当您使用“WHERE”子句时,它是否与 JOIN 或 INNER JOIN 相同?

Right now I'm thinking a stand-alone JOIN is identical to using commas and WHERE clauses.

现在我认为独立的 JOIN 与使用逗号和 WHERE 子句相同。

回答by Mark Byers

In MySQL writing JOINunqualified implies INNER JOIN. In other words the INNERin INNER JOINis optional. INNERand CROSSare synonyms in MySQL. For clarity I write JOINor INNER JOINif I have a join condition and CROSS JOINif I don't have a condition.

在 MySQL 中写JOIN不合格意味着INNER JOIN. 换句话说INNERinINNER JOIN是可选的。INNERCROSS是 MySQL 中的同义词。为了清楚起见,我写下JOIN或者INNER JOIN我是否有加入条件,CROSS JOIN如果我没有条件。

The allowed syntax for joins is described in the documentation.

文档中描述了允许的连接语法。



Right now I'm thinking a stand-alone JOIN is nothing more than (identical to) using commas and WHERE clauses.

现在我在想一个独立的 JOIN 只不过是(等同于)使用逗号和 WHERE 子句。



The effect is the same, but the history behind them is different. The comma syntax is from the ANSI-89 standard. However there are a number of problems with this syntax so in the ANSI-92 standard the JOIN keyword was introduced.

效果是一样的,但背后的历史却不同。逗号语法来自 ANSI-89 标准。然而,这种语法存在许多问题,因此在 ANSI-92 标准中引入了 JOIN 关键字。

I would strongly recommend that you alwaysuse JOIN syntax rather than the comma.

我强烈建议您始终使用 JOIN 语法而不是逗号。

  • T1 JOIN T2 ON ...is more readable than T1, T2 WHERE ....
  • It is more maintainable because the table relationships and filters are clearly defined rather than mixed together.
  • The JOIN syntax is easier to convert to OUTER JOIN than the comma syntax.
  • Mixing the comma and JOIN syntax in the same statement can give curious errors due to the precedence rules.
  • It is less likely to accidentally create a cartesian product when using the JOIN syntax due to a forgotten join clause, because the join clauses are written next to the joins and it is easy to see if one is missing.
  • T1 JOIN T2 ON ...比 更具可读性T1, T2 WHERE ...
  • 它更易于维护,因为表关系和过滤器被明确定义而不是混合在一起。
  • JOIN 语法比逗号语法更容易转换为 OUTER JOIN。
  • 由于优先规则,在同一语句中混合使用逗号和 JOIN 语法可能会产生奇怪的错误。
  • 使用 JOIN 语法时由于忘记了 join 子句而意外创建笛卡尔积的可能性较小,因为 join 子句写在连接旁边,很容易看出是否缺少一个。

回答by Mchl

These are all equivalent, and also equal to, CROSS JOIN.

这些都是等价的,也等于,CROSS JOIN

There are some differences between using comma and [INNER | CROSS] JOINsyntax, which might be important when joining more tables. Pretty much all you need to know is described here in the MySQL JOINdocumentation.

使用逗号和[INNER | CROSS] JOIN语法之间存在一些差异,这在加入更多表时可能很重要。您需要了解的几乎所有内容都在MySQLJOIN文档中进行了描述。