The difference between the inner join, left join, right join

 On the distinction between inner join and left join, formerly think they get to know the result is not expected in today found from the front to take the time parameters, before we know the problem is in the inner join up.

Demand data from the database search, the front-end display in the form of a bar graph, the data found by industry groups, show the number of households and the number of households accounted for each industry, there is a field related to the number of users A table, the total number of users and the industry name B table. Regardless of the investigation could have been no investigation of the data, the industry should show the name of the X-axis, the result is X, Y axis do not have any data. Problem is that I used the wrong way linking.

A, sql the left join, right join, join the distinction between inner

  left join (left coupling) Returns includes all left table records and the right table join fields are equal recording 
  right join (the right coupling) returns include all in the right table record and the left table join fields are equal recording
  inner join (like value of the connection) returns only two rows of the table is equal to the coupling field

For example as follows: 
--------------------------------------------
Table A record as follows:
aID in ANUM
. 1 a20050111
2 a20050112
. 3 a20050113
. 4 a20050114
. 5 a20050115

Table B recorded as follows:
bID bname
. 1 2006032401
2 2006032402
. 3 2,006,032,403
. 4 2006032404
. 8 2006032408

--------------------------------------------
1.left join
sql语句如下: 
select * from A
left join B 
on A.aID = B.bID

The results are as follows:
aID in ANUM bID bname
. 1 a20050111 2,006,032,401. 1
2 2 a20050112 2,006,032,402
. 3 a20050113 2,006,032,403. 3
. 4. 4 a20050114 2006032404
. 5 a20050115 NULL NULL

(Effect of the number of rows 5 rows)
results show:
. A left table records the Join is based, A can be seen as the left table, B can be seen at right, left join whichever is left table of
exchange words, the left table (a) record will be fully represented, while the right table (B) will only show records that meet the search criteria (for example: A.aID = B.bID).
place less than B table records are NULL.
--------------------------------------------
2. the Join right
SQL statement is as follows: 
SELECT * from A
right the Join B 
ON A.aID = B.bID

The results are as follows:
aID in ANUM bID bname
. 1 a20050111 2,006,032,401. 1
2 2 a20050112 2,006,032,402
. 3 a20050113 2,006,032,403. 3
. 4. 4 a20050114 2006032404
NULL NULL. 8 2,006,032,408

(Number of rows affected is 5 lines)
results show that:
a closer look, you will find, and the results left join just the opposite, this is the right table (B) based on the lack of local A table filled with NULL .
--------------------------------------------
3.inner the Join
SQL the following statement: 
SELECT * from A
INNER JOIN B 
ON A.aID = B.bID

The results are as follows:
aID in ANUM bID bname
. 1 a20050111 2,006,032,401. 1
2 2 a20050112 2006032402
. 3 a20050113 2006032403. 3
. 4. 4 2006032404 a20050114

The results Note:
Obviously, only here the record A.aID = B.bID this description is not to Who basic inner join, it only displays the matching records.

Guess you like

Origin www.cnblogs.com/ambitionutil/p/11420321.html