Home > Database > Mysql Tutorial > How do we differentiate between MySQL CROSS JOIN and INNER JOIN?

How do we differentiate between MySQL CROSS JOIN and INNER JOIN?

WBOY
Release: 2023-09-08 21:57:03
forward
1142 people have browsed it

我们如何区分 MySQL CROSS JOIN 和 INNER JOIN?

We can only distinguish MySQL CROSS JOIN and INNER JOIN based on the connection predicate (that is, the specified condition). While writing an INNER JOIN query, we need to specify the conditions, but conversely, while writing a CROSS JOIN query, we do not need to specify the conditions. 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)
Copy after login

Now, the query for INNER JOIN can be as follows-

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)
Copy after login

Now, if we Write the above query without specifying the conditions, then it will become a CROSS JOIN. The unconditional query is as follows -

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)
Copy after login

The above is the detailed content of How do we differentiate between MySQL CROSS JOIN and INNER JOIN?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template