假設我有一個用戶表和銷售訂單具有以下模式的表之後計數記錄:SQL/SQL-LITE - 過濾
- 客戶= {ID,名稱}
- sales_order的= { ID,CUSTOMER_ID,sales_representer}
用下面defintions:
- id爲兩個表中的主鍵。
- customer_id是一個foriegn關鍵客戶。
我要實現以下查詢:
For any customer whose sales_representer is 100, find the customer id,
customer name and the number of his overall orders.
我建立以下查詢:
select C.id, C.name, count(C.id)
from customer C, sales_order S
where C.id = S.customer_id and
S.sales_represntor = '100'
group by C.id, C.nname;
但作爲計數(C.id)結果我得到的只有數salesclient爲100的銷售額。 我知道我可以添加sales_order的另一個實例(即S2)並從中進行計數,但在我看來,這根本不算高效。
有沒有人有解決方案?
謝謝
以下解決方案是否也不錯?選擇C.id,C.name,從客戶C,sales_order的S,sales_order的TS 其中\t C.id = S.cust_id和 \t \t C.id = TS.cust_id和 \t \t計數(TS.cust_id) S.sales_rep ='100' group by C.id,C.name; – SyndicatorBBB
我希望你能理解上面寫的東西..我只是不知道如何在評論框中做一個新行。 – SyndicatorBBB
額外的連接會導致錯誤的(計數)記錄數。 –