MySQL - 没有 ON 子句的 JOIN vs CROSS JOIN vs SELECT t1.* t2.* FROM t1, t2;

MySQL - JOIN without ON clause vs CROSS JOIN vs SELECT t1.* t2.* FROM t1, t2;

我的问题是。以下三个查询是否会产生不同的输出?他们总是 return 相同的数据集还是有例外。它们在性能上有什么不同吗?

似乎在所有情况下,一个 table 的行乘以另一个 table 的行。

加入(没有“ON”子句)

SELECT * FROM 
t1 JOIN t2;

交叉连接

SELECT * FROM t1 
CROSS JOIN t2;

SELECT 来自两个表

SELECT a.*, b.*
FROM t1 a, t2 b;

示例数据:

CREATE TABLE t1(id integer, t CHAR(2));
INSERT INTO t1(id, t) 
VALUES
  (1, "t1"),
  (2, "t1"),
  (3, "t1"),
  (NULL, "t1")
;

CREATE TABLE t2(id integer, t CHAR(2));
INSERT INTO t2(id, t) 
VALUES
  (2, "t2"),
  (3, "t2"),
  (4, "t2"),
  (NULL, "t2")
;

所有查询将产生相同的输出:

+------+------+------+------+
| id   | t    | id   | t    |
+------+------+------+------+
|    1 | t1   |    2 | t2   |
|    2 | t1   |    2 | t2   |
|    3 | t1   |    2 | t2   |
| NULL | t1   |    2 | t2   |
|    1 | t1   |    3 | t2   |
|    2 | t1   |    3 | t2   |
|    3 | t1   |    3 | t2   |
| NULL | t1   |    3 | t2   |
|    1 | t1   |    4 | t2   |
|    2 | t1   |    4 | t2   |
|    3 | t1   |    4 | t2   |
| NULL | t1   |    4 | t2   |
|    1 | t1   | NULL | t2   |
|    2 | t1   | NULL | t2   |
|    3 | t1   | NULL | t2   |
| NULL | t1   | NULL | t2   |
+------+------+------+------+

所有三个表达式都是等价的,将产生相同的输出。正如 mysql join 上的手册所说:

In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace each other)

...

INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).