假設我有兩個表:如何選擇表和統計等表
客戶
Name | id |
-------------------
Benny | 1 |
Wilson | 2 |
Joe | 3 |
Austin | 4 |
訂單
Product | id |
---------------------
TV | 1 |
Hifi-set | 1 |
HTPC | 1 |
CD | 1 |
DVD | 1 |
CD | 1 |
DVD | 1 |
這就是我想要的結果:
Name | Orders |
-------------------
Benny | 7 |
Wilson | 0 |
Joe | 0 |
Austin | 0 |
我不熟悉wi日SQL,但我想:
SELECT c.Name FROM Customers AS c LEFT JOIN Orders AS o ON c.id=o.id GROUP BY c.Name
但有一個錯誤的結果:
Name | Orders |
-------------------
Benny | 4 |
Wilson | 1 |
Joe | 1 |
Austin | 1 |
我該怎麼辦?
RobB的嵌套'SELECT's查詢是完全有效的,但爲了記錄您的查詢可能是正確的,而不是您加入客戶ID以訂購ID。您可能需要'C.ID = O.CustomerID'或類似的。 – 2012-07-11 23:45:16