left join and right join essential difference (illustration)

1, test1 table

image.png

2, test2 table

image.png

3, the query

1, left join (1 condition table)

SELECT t1.id id1, t1.id1 a1, t1.id2 b1, t2.id1 a2, t2.id2 b2, t2.id id2
FROM test1 t1 LEFT JOIN test2 t2 ON t1.id1 = t2.id2 WHERE t1.id1 = 2

result:

image.png

2, left join (2 Table condition)

SELECT t1.id id1, t1.id1 a1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2  
FROM test1 t1 LEFT JOIN test2 t2 ON t1.id1 = t2.id2 WHERE t2.id1 = 2

result:

image.png

Summary: Whether the table to the left or right of the table are to the left of the main display, the left side of the table can be repeated

3, left join (there is no presence of the right to the left)

SELECT t1.id id1, t1.id1 a1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2  
FROM test1 t1 LEFT JOIN test2 t2 ON t1.id1 = t2.id2 WHERE t1.id1 = 6

result:

image.png

3, left join (left to the right there is absence of condition)

SELECT t1.id id1, t1.id1 a1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2  
FROM test1 t1 LEFT JOIN test2 t2 ON t1.id1 = t2.id2 WHERE t2.id1 = 5

result:

image.png

4, left join (query all the fields)

SELECT t1.id id1, t1.id1 a1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2  
FROM test1 t1 LEFT JOIN test2 t2 ON t1.id1 = t2.id2

result:

image.png

5, right join (query all the fields)

SELECT t1.id id1, t1.id1 a1, t1.id2 b1, t2.id id2, t2.id1 a2, t2.id2 b2  
FROM test1 t1 RIGHT JOIN test2 t2 ON t1.id1 = t2.id2

result:

image.png

to sum up:

Left and right connections on the contrary, is not introduced

1, ensure data integrity, and as long as the condition table corresponding to the left, will be displayed

2, and does not correspond to the left of the table, not displayed

3, there is shown on the left of the left, the right does not exist will not be displayed

[To facilitate their understanding of best knock over your hand, memories resonate through multiple organs, more profound memories]













Guess you like

Origin blog.51cto.com/14047445/2458670