The difference between left join, right join, inner join and full join in Mysql

The join query in sql has four ways: inner join, left join, right join, and full join . There is actually not much difference between them, just query The results are different.

It is roughly understood as this:


For example, we have two tables:
Write picture description here

The Orders table is related to the Persons table through the foreign key Id_P.

1. inner join (inner join), when the two tables are connected for query, only the result set that exactly matches in the two tables is retained.

We use inner join to connect and query the two tables, the sql is as follows:

SELECT p.LastName, p.FirstName, o.OrderNo
FROM Persons p
INNER JOIN Orders o
ON p.Id_P=o.Id_P and 1=1  --用and连接多个条件
ORDER BY p.LastName

Query result set:
Write picture description here

In this connection method, if the Id_P field in the Orders table cannot be found in the Persons table, it will not be listed.

Note: simply select * from a, b is the Cartesian product. For example, table a has 5 pieces of data and table b has 3 pieces of data, so the final result has 5*3=15 pieces of data.

But if you associate the two tables: select * from a, b where a.id = b.id, the meaning will change, and it is equivalent to:

select * from a inner join b on a.id = b.id. That is, the inner connection.

But this way of writing does not conform to the specification, and may only work for some databases, such as sqlserver. It is recommended not to write this way. It's best to write it as inner join.

The difference between inner join query (select * from a join b on a.id = b.id) and associated query (select * from a, b where a.id = b.id)

 

2. Left join, when the two tables are connected for query, all rows of the left table will be returned, even if there is no matching record in the right table.

We use left join to connect and query the two tables, the sql is as follows:

SELECT p.LastName, p.FirstName, o.OrderNo
FROM Persons p
LEFT JOIN Orders o
ON p.Id_P=o.Id_P
ORDER BY p.LastName

The query result is as follows:
Write picture description here
you can see that the Id_P field of the row whose LastName is Bush in the left table (Persons table) does not match in the right table (Orders table), but the query result still retains the row.

 

3. Right join, when the two tables are connected and inquired, all rows of the right table will be returned, even if there is no matching record in the left table.

We use right join to connect and query the two tables, the sql is as follows:

SELECT p.LastName, p.FirstName, o.OrderNo
FROM Persons p
RIGHT JOIN Orders o
ON p.Id_P=o.Id_P
ORDER BY p.LastName

The query results are as follows:

Write picture description here
The Id_P field value of the last record in the Orders table is 65. There is no record matching it in the left table, but it is still retained.

 

4.full join, when the two tables are connected and query, all the rows that do not match in the left and right tables are returned (there is full join in oracle, but there is no full join in mysql)

We use full join to connect and query the two tables, the sql is as follows:

SELECT p.LastName, p.FirstName, o.OrderNo
FROM Persons p
FULL JOIN Orders o
ON p.Id_P=o.Id_P
ORDER BY p.LastName

The query result is as follows: the
Write picture description here
query result is the union of left join and right join.

The difference between these connection queries is nothing more.

Guess you like

Origin blog.csdn.net/weixin_43452467/article/details/113523788