Home > Database > Mysql Tutorial > body text

mysql的left join、right join、inner join

王林
Release: 2019-08-23 15:14:38
forward
3142 people have browsed it

A gas card table:

id, userName, cardNo

1                                                                                                                                                                                                aaa 222

BRefueling record table:

id, number, userName , cardNo,

1 1234 111

##2 234 bbb left join:

select * from B   b    left join  A a on a.userName = b.userName   where b.userName=aaa
Copy after login

Due to the condition after on in the above SQL, userName corresponds to multiple entries in table A instead of one, and the result set is a Cartesian product. One item in table B satisfies the remaining two items in table A. The result is 2.

select * from B   b    left join  A a on a.userName = b.userName  and a.cardNo = b.cardNo  where b.userName=aaa
Copy after login

Since in the above SQL, the two conditions after on can only find one unique piece of data in table A, so the result is how many pieces of data in table B satisfy where, and the result set will return how many pieces of data. Here is a piece of data returned

right join:

The following sql has the same effect as the above left join:

select * from A   a    right join  B b on a.userName = b.userName  and a.cardNo = b.cardNo  where b.userName=aaa
Copy after login

inner join:

select * from A   a    inner  join  B b on a.userName = b.userName  and a.cardNo = b.cardNo  where a.userName=aaa
Copy after login
Let’s first look at the conditions after on. If a piece of data in table A corresponds to the two conditions of on and there is only one piece of data in B, then 2 pieces of data that satisfy the where condition are returned.

select * from B   b    inner  join  A a on a.userName = b.userName  and a.cardNo = b.cardNo  where a.userName=aaa
Copy after login

To summarize the above: Look at the condition behind on to see whether there is one or more pieces of data in the related table.

For more related questions, please visit the PHP Chinese website:

mysql video tutorial

The above is the detailed content of mysql的left join、right join、inner join. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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