Detailed explanation of sql's left join (LEFT JOIN), right join (RIGHT JOIN) and inner join (INNER JOIN)

Detailed explanation of sql's left join (LEFT JOIN), right join (RIGHT JOIN) and inner join (INNER JOIN):

Here is an example of connecting two tables:


Create table 1: as a personnel table, here it is regarded as the left table;

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

Create table 2: for the published article table, here it is regarded as the right table;

CREATE TABLE `news` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `types` varchar(255) DEFAULT NULL,
  `titles` varchar(255) DEFAULT NULL,
  `contents` varchar(255) DEFAULT NULL,
  `author` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

1. Left join (LEFT JOIN) is called left outer join:

Based on the left table, the two tables are connected according to the conditions of the two tables given after ON.
As a result, all the query data in the left table will be displayed, while the right table will only display the conditions after ON and the part satisfied by the left table.

Example: use the username field of the left table and the author field of the right table as the connection fields of the two tables to connect the two tables;

select user.username,user.phone,news.types,news.author,news.titles from user 
left join news on user.username = news.author

Figure 1

 

It can be seen from the figure that the data in the left table is displayed, while the data in the right table only shows the data connected to the left table; if the data in the left table
and the data in the right table are one-to-many, then the right table The data corresponding to one item in the left table is displayed.

2. Right join (RIGHT JOIN) is called right outer join:

Based on the right table, the two tables are connected according to the conditions of the two tables given after ON.
As a result, all the query data in the right table will be displayed, while the left table will only display the conditions after ON and the part satisfied by the right table.

Example: use the username field of the left table and the author field of the right table as the connection fields of the two tables to connect the two tables;

select user.username,user.phone,news.types,news.author,news.titles from user 
right join news on user.username = news.author

Figure 2

 

It can be seen from the figure that the data in the right table is displayed, while the data in the left table only shows the data connected to the right table; if the data in the right table
and the data in the left table are many-to-one, then the left table One piece of data corresponding to multiple pieces in the right table will be displayed repeatedly.

3. INNER JOIN

It is to use two tables as reference objects at the same time, and connect the two tables according to the conditions of the two tables given after ON.
The result is that only the parts of the two tables that meet the conditions behind ON will be displayed.

Example: use the username field of the left table and the author field of the right table as the connection fields of the two tables to connect the two tables;

select user.username,user.phone,news.types,news.author,news.titles from user 
inner join news on user.username = news.author

Figure 3

 

It can be seen from the figure that only the part where the left table and the right table are connected with the same data will be displayed.
 

Guess you like

Origin blog.csdn.net/qq_34297287/article/details/130143003