SQL:如何显示一个表中的所有行,这些行未在所连接的第二个表中列出

时间:2014-03-21 05:32:18

标签: sql join

我很难将所有行显示在'客户'表,因为它们并未全部列在' SalesOrder'我加入了桌子。

如果Customer表的值为0,我如何从Customer表中显示所有行?感谢您的任何建议:)

SELECT Customer.CID, Customer.Name, 
COUNT (SalesOrder.CID) AS NbrOrders, 
SUM (FullPrice) AS [Total Value of Items]
FROM (Customer
  INNER JOIN SalesOrder
  ON Customer.CID=SalesOrder.CID)
GROUP BY Customer.CID, Customer.Name;

2 个答案:

答案 0 :(得分:1)

使用LEFT JOIN而不是INNER JOIN

SELECT Customer.CID, Customer.Name, 
COUNT (SalesOrder.CID) AS NbrOrders, 
SUM (FullPrice) AS [Total Value of Items]
FROM Customer
  LEFT JOIN SalesOrder
  ON Customer.CID=SalesOrder.CID
GROUP BY Customer.CID, Customer.Name;

答案 1 :(得分:0)

使用左连接。这将显示第二个表中没有第一个表中相应数据的值。