Home >Database >Mysql Tutorial >How to write a cross join MySQL query with the help of comma operator?

How to write a cross join MySQL query with the help of comma operator?

王林
王林forward
2023-09-18 08:01:08923browse

如何借助逗号运算符编写交叉连接的 MySQL 查询?

#Writing a cross join using the comma operator is the most basic way to combine two tables. We know that we can also write cross joins using keyword CROSS JOIN or synonyms like JOIN. In order to form a cross join, we do not need to specify a condition called a join predicate. To understand it, let us take the example of two tables named tbl_1 and tbl_2, which have the following data -

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)

Now, the following query will cross join the above tables using comma operator.

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)

The above is the detailed content of How to write a cross join MySQL query with the help of comma operator?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete