Difference between right join and left join

Reposted from https://www.cnblogs.com/assasion/p/7768931.html

Regarding the difference between inner join and left join, I thought I understood it before. Today, when I took the parameters from the front end, I found that it was not the expected result, so I realized that the problem lies in the inner join.

The requirement is to check data from the database and display it in the form of a column chart on the front end. The found data is grouped by industry, showing the number of households and the proportion of households in each industry. The fields involved include the number of users in table A, The total number of users and the name of the industry in Table B. Originally, no matter whether the data is checked or not, the industry name should be displayed on the X-axis, but the result is that there is no data displayed on the X-axis and Y-axis. The problem was that I was using the wrong connection method.

1. The difference between left join, right join and inner join of sql

left join (left join) returns records including all records in the left table and the records with the same join fields in the right table right
  join (right join) returns records including all records in the right table and the join fields in the left table with equal records
  inner join (etc. Join by value) returns only rows where the joined fields in the two tables are equal

Examples are as follows:

Table A is recorded as follows:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115

Table B is recorded as follows:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408


1. The left join
sql statement is as follows:
select * from A
left join B
on A.aID = B.bID

The result is as follows:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 NULL NULL

(The number of rows affected is 5 rows)
The result shows:
the left join is based on the records of table A, A can be regarded as the left table, B can be regarded as the right table, and the left join is based on the left table
. In other words, all the records in the left table (A) will be displayed, while the right table (B) will only display the records that meet the search criteria (in the example: A.aID = B.bID). The lack of records in table
B are all NULL.

2. The right join
sql statement is as follows:
select * from A
right join B
on A.aID = B.bID

The result is as follows:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL NULL 8 20060 32408

(The number of rows affected is 5 rows)
Result description:
If you look carefully, you will find that the result of left join is just the opposite. This time it is based on the right table (B), and the insufficient parts of A table are filled with NULL .

3. The inner join
sql statement is as follows:
select * from A
innerjoin B
on A.aID = B.bID

The result is as follows:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404

Result description:
Obviously, only the records of A.aID = B.bID are displayed here. This shows that the inner join is not based on who, it only displays the records that meet the conditions.

Guess you like

Origin blog.csdn.net/LIARRR/article/details/89442396