Comparison of join, left outer join, right join, and inner join in the database

First, we use the following statements to create the relevant tables of the database.

create table user
(
	id int primary key,
	name varchar(20) not null,
	age int
);
insert into user(id,name,age)
values
(1,"Marry",20),
(2,"Mike",18),
(3,"Nike",23);

create table `order`
(
	id int primary key,
	description varchar(50),
	uid int
);

insert into  `order`
values(1001,"table",1),(1002,"ball",1),(1003,"key",2),(1004,"football",7);

Let's query the data stored in the database


We first perform the join operation

select a.name,b.description from user a join `order` b on a.id=b.uid;

The result obtained is the above figure

It can be seen that the join operation is mainly in both tables, and the main basis for the connection of these two tables is id.

Now do a left outer join

select a.name,b.description from user a left outer join `order` b on a.id=b.uid;

The result obtained is


From the above results, we can conclude that the fields of the left table will all appear. For the operation that is not connected, that is to say, the right table has no value corresponding to it, it will be set to NULL here.

for right join

We use the following sql statement

select a.name,b.description from user a right outer join `order` b on a.id=b.uid;


For the above results, we can conclude that for the right outer join, the main thing is to fetch all the elements in the right table, and if there is no corresponding in the left table, null will be used instead.

Finally, let's take a look at the inner join


It can be seen that inner join and join are the same, there is no difference

Summarize

Some students may see words such as left join and right join, which are equivalent to left outer join and right outer join. We can see from the above example that left join retains the left table, that is, all elements of the left table Used to join, right join is just the opposite, for inner join and join, here is just the intersection of two tables.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684573&siteId=291194637