SQL查询是加入我的问题吗?

时间:2014-12-17 02:02:20

标签: sql sql-server sql-server-2005

包含table1中的所有记录,仅包含来自table2的ID字段相等的记录。

Select
Count(uno.itemID) As [Merchandise Sold],
uno.salesmanName As [Who Got Sale],
COUNT(duo.SalesCancelled) As [Bring it Back]
FROM table1 uno
INNER JOIN table2 duo
ON uno.itemID = duo.itemID
Group By uno.salesmanName

我尝试过内心,右边,左边,但没有得到预期的结果

1 个答案:

答案 0 :(得分:1)

我想你想要一个left join,因为你说你想把所有内容保存在第一个表中:

Select Count(uno.itemID) As [Merchandise Sold],
       uno.salesmanName As [Who Got Sale],
       COUNT(duo.SalesCancelled) As [Bring it Back]
FROM table1 uno LEFT JOIN
     table2 duo
     ON uno.itemID = duo.itemID
Group By uno.salesmanName;