在逗号运算符的帮助下编写交叉联接是组合两个表的最基本方法。众所周知,我们还可以使用关键字CROSS JOIN或诸如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)
现在,下面的查询将使用逗号运算符交叉连接上述表-
mysql> Select * FROM tbl_1,tbl_2 ; +----+--------+----+---------+ | Id | Name | Id | Name | +----+--------+----+---------+ | 1 | Gaurav | A | Aarav | | 2 | Rahul | A | Aarav | | 3 | Raman | A | Aarav | | 4 | Aarav | A | Aarav | | 1 | Gaurav | B | Mohan | | 2 | Rahul | B | Mohan | | 3 | Raman | B | Mohan | | 4 | Aarav | B | Mohan | | 1 | Gaurav | C | Jai | | 2 | Rahul | C | Jai | | 3 | Raman | C | Jai | | 4 | Aarav | C | Jai | | 1 | Gaurav | D | Harshit | | 2 | Rahul | D | Harshit | | 3 | Raman | D | Harshit | | 4 | Aarav | D | Harshit | +----+--------+----+---------+ 16 rows in set (0.00 sec)