Home > Database > Mysql Tutorial > body text

What will MySQL return if I don't use the keyword 'RIGHT' or 'LEFT' when I write a RIGHT JOIN or LEFT JOIN query?

WBOY
Release: 2023-08-25 18:05:06
forward
1305 people have browsed it

如果我在编写 RIGHT JOIN 或 LEFT JOIN 查询时不使用关键字“RIGHT”或“LEFT”,MySQL 将返回什么?

In both cases, when the "RIGHT" or "LEFT" keywords are not used in the query, MySQL will return the results as an INNER JOIN query. This is because the only difference between RIGHT, LEFT, and INNER JOIN is the RIGHT or LEFT keyword. To understand it, let's take an 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 RIGHT JOIN using keyword RIGHT can be as follows -

mysql> SELECT tbl_1.id,tbl_2.id FROM tbl_1 RIGHT JOIN tbl_2 ON tbl_1.name = tbl_2.name;
+------+----+
| id   | id |
+------+----+
| 4    | A  |
| NULL | B  |
| NULL | C  |
| NULL | D  |
+------+----+
4 rows in set (0.00 sec)
Copy after login

Now, in the following query we are not using the keyword RIGHT -

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

From the above result set we can observe the difference, without using the keyword 'RIGHT', MySQL will treat it as INNER JOIN the query and return the results accordingly.

Now, the query for LEFT JOIN using the keyword LEFT can be as follows-

mysql> SELECT tbl_1.id,tbl_2.id FROM tbl_1 LEFT JOIN tbl_2 ON tbl_1.name = tbl_2.name;
+----+------+
| id | id   |
+----+------+
| 1  | NULL |
| 2  | NULL |
| 3  | NULL |
| 4  | A    |
+----+------+
4 rows in set (0.02 sec)
Copy after login

Now, in the following query we do not use the keyword LEFT-

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

From the above We can observe the difference in the result set, without using the keyword "LEFT", MySQL treats it as an INNER JOIN query and returns the results accordingly.

The above is the detailed content of What will MySQL return if I don't use the keyword 'RIGHT' or 'LEFT' when I write a RIGHT JOIN or LEFT JOIN query?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!