如何在没有ON条件的情况下使用mysql JOIN?
是否可以在没有ON
语句的情况下编写联接查询?以及这些联接的不同之处LEFT JOIN, RIGHT JOIN
的工作原理.
Is it possible to write join query without ON
statement? and how do these joins differ LEFT JOIN, RIGHT JOIN
works.
MySQL 文档涵盖了这个主题.
MySQL documentation covers this topic.
这里是提要.使用join
或inner join
时,on
条件是可选的.这与ANSI标准不同,并且与几乎所有其他数据库不同.效果是cross join
.同样,您可以在cross join
中使用on
子句,该子句也与标准SQL不同.
Here is a synopsis. When using join
or inner join
, the on
condition 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 on
clause with cross join
, which also differs from standard SQL.
交叉连接将创建笛卡尔乘积-即,第一个表中的第一行和第二个表中的第一行的每种可能组合.具有三行('a','b'和'c')的表和具有四行(例如1、2、3、4)的表的交叉联接将具有12行.
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.
在实践中,如果要进行交叉联接,请使用cross join
:
In practice, if you want to do a cross join, then use cross join
:
from A cross join B
比:
from A, B
和:
from A join B -- with no on clause
on
子句是左右外部联接所必需的,因此讨论与它们无关.
The on
clause is required for a right or left outer join, so the discussion is not relevant for them.
如果您需要了解联接的不同类型,则需要对关系数据库进行一些研究.在该级别的讨论中,Stackoverflow不合适.
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.