Ha, I haven’t updated this article for a long time. Today, I will talk about the small things about mySQL in those years. Left join, join, right join, inner join, etc. One of the most common things you’ll find on the Internet is a graphic that looks like this:



Is really a picture all understand the difference between the join ah, it’s a pity that I still don’t understand, may people are lazy, then basic a left join is enough for me, so I didn’t go to carefully studied, but the reality was forced me to find out, just myself, finally understand the meaning in the graph, the following will listen to my moments.

First, let’s create two tables, the first table named kemu, the second table named score:

Table 1 is left joined to Table 2, with the left as the main part, indicating that Table 1 is the main part, and the data in Table 2 above is associated. The results obtained show all the data on the left, and the data on the right is displayed with the part that has intersection with the left. As follows:

select
   *
from
   kemu
left join score on kemu.id = score.idCopy the code

The result set:

Second, the right to join

“Right join”, table 1 right join Table 2, with the right as the main, indicating that Table 2 is the main, associated query data in Table 1, find all data in Table 2 and data with intersection between Table 1 and Table 2, as follows:

select
   *
from
   kemu
right join score on kemu.id = score.idCopy the code

The result set:

Inner join means the intersection of two tables. It is found that the part of two tables have intersection, and the rest are not associated. This is also used in many cases, as follows

select
   *
from
   kemu
join score on kemu.id = score.idCopy the code

The result set:

That’s the difference between the three kinds of connections!

A BLOG address:www.liangsonghua.com

Pay attention to wechat public number: songhua preserved egg bulletin board, get more exciting!

Introduction to our official account: We share our technical insights from working in JD, as well as JAVA technology and best practices in the industry, most of which are pragmatic, understandable and reproducible