2012-11-01 130 views
0

我有一個SQL查詢我的MYSQL數據庫,並希望限制結果只有10個銷售人員,而不是20.我試圖在查詢中插入他們的名字,但不斷收到錯誤。也可以根據總數排名。我使用的SQL查詢:限制sql查詢和排名總計

select sales_id, totalbuy, totalsell, totalbuy + totalsell as total from 
(select sales_id, SUM(CASE WHEN side= 'buy' THEN 1 ELSE 0 END) AS totalbuy, 
SUM(CASE WHEN side= 'sell' THEN 1 ELSE 0 END) AS totalsell 
from sales_id where sales_id='sales1', GROUP BY sales_id)q 

,它說的銷售額1我已經嘗試了其餘9個sales_ids'使用逗號和單引號補充,但不能讓它開始工作。

回答

0

如果您只需要某些sales_id,請使用「IN」子句。

如果你想只有前10總,刪除了這一行,並使用極限

select sales_id, totalbuy, totalsell, totalbuy + totalsell as total from 
(select owner_id, SUM(CASE WHEN side= 'buy' THEN 1 ELSE 0 END) AS totalbuy, 
SUM(CASE WHEN side= 'sell' THEN 1 ELSE 0 END) AS totalsell 
from sales_id 
where sales_id in ('sales1', 'sales2', 'sales3', 'sales4')-- if you wanna certain sales_id only 
GROUP BY sales_id)q 
order by total desc 
limit 0, 10;--top 10 by total (bigger first) 
+0

我得到這個錯誤檢查對應於你的MySQL服務器版本使用附近的「GROUP BY sales_id正確的語法手冊)q order by total desc 限制0,10'在第4行 – userH

+0

您的STAR!非常感謝你! – userH