作者:WSF15WEDFV_564 | 来源:互联网 | 2023-09-25 12:50
innerjoin:TheINNERJOINkeywordselectsallrowsfrombothtablesaslongasthereisamatchbetweenthe
inner join
:
The INNER JOIN keyword selects all rows
from both tables as long as there is a match between the columns in both
tables.
>
left join :
The LEFT JOIN keyword returns all rows
from the left table (table1), with the matching rows in the right table
(table2). The result is NULL in the right side when there is no
match.
>
以 sql server NorthWind 数据库为例
: 如果用 inner join
SELECT Customers.CompanyName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CompanyName;
得到
Alfreds Futterkiste 10643
Alfreds
Futterkiste 10692
Alfreds Futterkiste
10702
Alfreds Futterkiste
10835
Alfreds Futterkiste
10952
Alfreds Futterkiste
11011
Ana Trujillo Emparedados y helados
10308
Ana Trujillo Emparedados y helados 10625
Ana
Trujillo Emparedados y helados 10759
Ana Trujillo
Emparedados y helados 10926
Antonio Moreno
Taquería 10365
Antonio Moreno Taquería
10507
Antonio Moreno Taquería
10535
Antonio Moreno Taquería 10573
Antonio Moreno
Taquería 10677
Antonio Moreno Taquería
10682
............ 共830 条记录
,意思是查出所有 有销售记录的customer 及其 所有的orderID
如果用 left join
SELECT Customers.CompanyName, Orders.OrderID
FROM Customers
left JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CompanyName;
会得到 832 条记录 , 比 inner
join 多2条是因为: 在customer表中, 有2个customer 并无销售order , 但因为是left
join ,
这两个customer 也被包括在结果集之中。
left join 与 left outer join 无区别 , 是后者的简写 。