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

发布于 2024-10-07 14:54:35 字数 321 浏览 2 评论 0原文

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

MySQL 中默认的 JOIN 是什么?

SELECT * FROM t1 JOIN t2

这与另一个相关问题相同吗

SELECT * FROM t1, t2

OR

SELECT * FROM t1 INNER JOIN t2

,当您使用“WHERE”子句时,它与 JOIN 或 INNER JOIN 相同吗?

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

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?

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 ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

时光暖心i 2024-10-14 14:54:35

在 MySQL 中,编写 JOIN 不合格意味着 INNER JOIN。换句话说,INNER JOIN 中的INNER 是可选的。 INNERCROSS 在 MySQL 中是同义词。为了清楚起见,如果我有连接条件,我会写 JOININNER JOIN ;如果我没有条件,我会写 CROSS JOIN

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


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


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

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

  • T1 JOIN T2 ON ...T1, T2 WHERE ... 更具可读性。
  • 它更易于维护,因为表关系和过滤器是明确定义的而不是混合在一起。
  • JOIN 语法比逗号语法更容易转换为 OUTER JOIN。
  • 由于优先级规则,在同一语句中混合使用逗号和 JOIN 语法可能会产生奇怪的错误。
  • 使用 JOIN 语法时,由于忘记连接子句而意外创建笛卡尔积的可能性较小,因为连接子句写在连接旁边,很容易看出是否缺少连接子句。

In MySQL writing JOIN unqualified implies INNER JOIN. In other words the INNER in INNER JOIN is optional. INNER and CROSS are synonyms in MySQL. For clarity I write JOIN or INNER JOIN if I have a join condition and CROSS JOIN if I don't have a condition.

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.


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.

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

  • 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.
翻了热茶 2024-10-14 14:54:35

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

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

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

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

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