引自w3School.
left join 会从左表查询所有的数据,即使右表没有匹配的行
原始的表 (用在例子中的):
“Persons” 表:
Id_P LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
“Orders” 表:
Id_O OrderNo Id_P
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 65
左连接 和 内连接的比较
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName
用左连接查出的数据右表即使没有Bush的订单还是会查出来Bush的数据,对应的订单数为null
Adams John 22456
Adams John 24562
Bush George null
Carter Thomas 77895
Carter Thomas 44678
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons ,orders
where Persons.Id_P=orders.Id_P
ORDER BY Persons.LastName
用内连接查出的数据,不在包含Bush,因为Bush并没有订单
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678