Get the App
SLTechnology News&Howtos  ›  Database  › 

What's the difference between mysql connecting left join, right join and inner join?

Shulou Source: shulou.com Published: 2022-06-01 19:45:34 11月16日 Update

The following is followed by the author to understand the difference between mysql and left join, right join and inner join. I believe you will benefit a lot after reading it. The text is not much in essence. I hope that mysql connects left join, right join and inner join. What is the difference between this short article? you want.

A gas card meter:

Id, userName, cardNo

1 aaa 111

2 bbb 111

3 aaa 222

B refueling record sheet:

Id, number, userName, cardNo

1 1234 aaa 111

2 234 bbb 111

Left join:

Select * from B b left join An an on a.userName = b.userName where b.userName=aaa

Because of the condition after on in sql above, userName corresponds to more than one item in A table, and the result set is Cartesian product. One item in table B satisfies the two items in the remaining table A. The results were 2.

Select * from B b left join An an on a.userName = b.userName and a.cardNo = b.cardNo where b.userName=aaa

Because in the above sql, only one unique piece of data can be found in table A under the two conditions followed by on, so the result is that the result set returns as many pieces of data in table B satisfy where. Here is to return a piece of data

Right join:

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

Select * from An a right join B b on a.userName = b.userName and a.cardNo = b.cardNo where b.userName=aaa

Inner join:

Select * from An an inner join B b on a.username = b.userName and a.cardNo = b.cardNo where a.userName=aaa

First of all, let's look at the conditions after on. If a piece of data in table A corresponds to two conditions of on and there is only one piece of data in B, two pieces of data that meet the where condition are returned.

Select * from B b inner join An an on a.username = b.userName and a.cardNo = b.cardNo where a.userName=aaa

After reading this article about the difference between mysql and left join, right join and inner join, many readers will definitely want to know more about it. For more industry information, you can follow our industry information section.

Tags: Data conditions results two content that is more industry affirmation benefit a lot information only multiple effects text friends columns author article record table Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL NVidia Shulou Tech Info Docker