1, test1表sql
2,test2表ide
3,查詢
3d
1,left join (1表爲條件)blog
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
結果:
it
2,left join (2表爲條件)class
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
結果:test
總結:不管是左邊 的表仍是右邊的表都以顯示左邊的爲主,左邊的表能夠重複im
3,left join (左邊存在右邊不存在條件)總結
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
結果:
數據
3,left join (右邊存在左邊不存在條件)
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
結果:
4,left join (查詢全部的字段)
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
結果:
5,right join (查詢全部字段)
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
結果:
總結:
左鏈接和右鏈接正好相反,不介紹了
1,保證數據的完整,只要和左邊的表條件對應,都會顯示
2,和左邊的表不對應,就不顯示
3,左邊存在顯示左邊的,右邊不存在就不顯示
[便於本身理解最好本身手動敲一遍,記憶是經過多個器官共鳴,記憶更加深厚]