如何在没有 ON 条件的情况下使用 mysql JOIN?

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

How to use mysql JOIN without ON condition?

mysqljoinleft-joininner-joinright-join

提问by Alexander T.

Is it possible to write join query without ONstatement? and how do these joins differ LEFT JOIN, RIGHT JOINworks.

是否可以在没有ON语句的情况下编写连接查询?以及这些连接如何不同LEFT JOIN, RIGHT JOIN

回答by Gordon Linoff

MySQL documentationcovers this topic.

MySQL文档涵盖了这个主题。

Here is a synopsis. When using joinor inner join, the oncondition is optional. This is different from the ANSI standard and different from almost any other database. The effect is a cross join. Similarly, you can use an onclause with cross join, which also differs from standard SQL.

这是一个概要。使用join或 时inner joinon条件是可选的。这不同于 ANSI 标准,也不同于几乎任何其他数据库。效果是一个cross join。同样,您可以使用onwith 子句cross join,这也不同于标准 SQL。

A cross join creates a Cartesian product -- that is, every possible combination of 1 row from the first table and 1 row from the second. The cross join for a table with three rows ('a', 'b', and 'c') and a table with four rows (say 1, 2, 3, 4) would have 12 rows.

交叉连接创建笛卡尔积——即第一个表中的 1 行和第二个表中的 1 行的所有可能组合。具有三行('a'、'b' 和 'c')的表和具有四行(例如 1、2、3、4)的表的交叉联接将有 12 行。

In practice, if you want to do a cross join, then use cross join:

在实践中,如果你想做一个交叉连接,那么使用cross join

from A cross join B

is much better than:

比:

from A, B

and:

和:

from A join B -- with no on clause

The onclause is required for a right or left outer join, so the discussion is not relevant for them.

on右外连接或左外连接需要该子句,因此讨论与它们无关。

If you need to understand the different types of joins, then you need to do some studying on relational databases. Stackoverflow is not an appropriate place for that level of discussion.

如果您需要了解不同类型的联接,那么您需要对关系数据库进行一些研究。Stackoverflow 不适合进行这种级别的讨论。

回答by Manu

See some example in http://www.sitepoint.com/understanding-sql-joins-mysql-database/

请参阅http://www.sitepoint.com/understanding-sql-joins-mysql-database/ 中的一些示例

You can use 'USING' instead of 'ON' as in the query

您可以在查询中使用 'USING' 而不是 'ON'

SELECT * FROM table1 LEFT JOIN table2 USING (id);

回答by TARKUS

You might also want to investigate the use of UNION, which combines multiple tables for one output: SQL - Combine two tables for one output

您可能还想研究 UNION 的使用,它将多个表组合为一个输出:SQL - 将两个表组合为一个输出