使用逗号运算符编写交叉连接是将两个表组合的最基本方法。我们知道,我们还可以使用关键字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)
以上是如何借助逗号运算符编写交叉连接的 MySQL 查询?的详细内容。更多信息请关注PHP中文网其他相关文章!