Home  >  Article  >  Database  >  How to write MySQL cross join query with the help of keyword JOIN?

How to write MySQL cross join query with the help of keyword JOIN?

PHPz
PHPzforward
2023-09-07 12:09:10632browse

如何借助关键字 JOIN 编写 MySQL 交叉连接查询?

Cross-joined MySQL queries can also be written with the help of the keyword JOIN. To understand it, let's 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 tables using the keyword 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)

The above is the detailed content of How to write MySQL cross join query with the help of keyword JOIN?. 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