我们只能根据连接谓词(即指定的条件)来区分 MySQL CROSS JOIN 和 INNER JOIN。在编写 INNER JOIN 查询时,我们需要指定条件,但相反,在编写 CROSS JOIN 查询时,我们不需要指定条件。为了理解它,我们以名为 tbl_1 和 tbl_2 的两个表为例,它们具有以下数据 -
mysql> Select * from tbl_1; +----+--------+ | Id | Name | +----+--------+ | 1 | Gaurav | | 2 | Rahul | | 3 | Raman | | 4 | Aarav | +----+--------+ 4 rows in set (0.00 sec) mysql> Select * from tbl_2; +----+---------+ | Id | Name | +----+---------+ | A | Aarav | | B | Mohan | | C | Jai | | D | Harshit | +----+---------+ 4 rows in set (0.00 sec)
现在,INNER JOIN 的查询可以如下 -
mysql> SELECT tbl_1.id,tbl_2.id FROM tbl_1 JOIN tbl_2 ON tbl_1.name = tbl_2.name; +----+----+ | id | id | +----+----+ | 4 | A | +----+----+ 1 row in set (0.00 sec)
现在,如果我们在不指定条件的情况下编写上述查询,那么它将变成 CROSS JOIN。无条件查询如下 -
mysql> Select tbl_1.id, tbl_2.id FROM tbl_1 JOIN tbl_2; +----+----+ | id | id | +----+----+ | 1 | A | | 2 | A | | 3 | A | | 4 | A | | 1 | B | | 2 | B | | 3 | B | | 4 | B | | 1 | C | | 2 | C | | 3 | C | | 4 | C | | 1 | D | | 2 | D | | 3 | D | | 4 | D | +----+----+ 16 rows in set (0.00 sec)
以上是我们如何区分 MySQL CROSS JOIN 和 INNER JOIN?的详细内容。更多信息请关注PHP中文网其他相关文章!