[MySQL] Comparison of the difference between left join, right join, and inner join

Original link: https://segmentfault.com/a/1190000017369618

Ha, I haven't updated the article for a long time, today I will talk about the little things about mySQL in those years. Speaking of mySQL, I have been using it for a long time, but there is a problem that has been bothering me, that is, the difference between various joins such as left join, join, right join, and inner join. When searching online, the most common one is a diagram, as follows:

clipboard.png

It's really a picture that tells the difference between all joins, but unfortunately I still don't understand it, maybe people are lazy, and basically a left join is enough for me, so I didn't study it carefully, but the reality Still forced me to figure it out, simply do it myself, and finally understand the meaning of the picture, let me listen to me one by one below.

First, let's create two tables, the first table is named kemu, and the second table is named score:

clipboard.pngclipboard.png

1.left join

As the name implies, it is "left connection". Table 1 is left connected to Table 2, and left is the main one. It means table 1 is the main one. The data in table 2 above is associated. The result of the check shows all the data on the left, and then the right shows and On the left is the data of the intersection. as follows:

select
   *
from
   kemu
left join score on kemu.id = score.id

Result set:
clipboard.pngclipboard.png

2.right join

"Right connection", Table 1 is connected to Table 2 on the right, and the right is dominant, which means that Table 2 is the main one, and the data in Table 1 is correlated to find out all the data in Table 2 and the intersection data of Table 1 and Table 2, as follows:

select
   *
from
   kemu
right join score on kemu.id = score.id

Result set:

clipboard.pngclipboard.png

3.join

Join is actually "inner join". It is written as join for abbreviation. Two means one. Inner join means that the intersection of the two tables is the main one. It is found that the two tables have intersections, and the rest are not related. Without additional display, this is also used in many cases, as follows

select
   *
from
   kemu
join score on kemu.id = score.id

Result set:

clipboard.pngclipboard.png

The above is the difference between the three connections!

Guess you like

Origin blog.csdn.net/weixin_43935927/article/details/113983460