Home > Database > Mysql Tutorial > body text

What is MySQL RIGHT JOIN and how do we write MySQL queries for it?

PHPz
Release: 2023-09-05 15:09:02
forward
1595 people have browsed it

什么是 MySQL RIGHT JOIN 以及我们如何为其编写 MySQL 查询?

It is similar to LEFT join, except that the table processing is reversed. Using a RIGHT join, every row from the right table will appear in the result set. For rows in the right table that do not match rows in the left table, the columns in the left table will display NULL. 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 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

The above is the detailed content of What is MySQL RIGHT JOIN and how do we write MySQL queries for it?. 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