1. 程式人生 > >sql中left join、right join、inner join之間的區別

sql中left join、right join、inner join之間的區別

以persons表和orders表為例進行直觀的解釋

persons表如下:


orders表如下:


SELECT * FROM persons INNER JOIN orders ON persons.Id_P = orders.Id_P

結果:


結論:

inner join並不以誰為基礎,它只顯示符合條件的全部記錄

SELECT * FROM persons LEFT JOIN orders ON persons.Id_P = orders.Id_P

結果:


結論:

以左表為基準,意思就是左表的資訊必須顯示全,右表有的資料與左表沒實現對應的就不顯示。

SELECT * FROM persons RIGHT JOIN orders ON persons.Id_P = orders.Id_P

結果:


結論:

以右表為基準,意思就是右表的資訊必須顯示全,左表有的資料與右表沒實現對應的就不顯示。