我有一個表格,其中包含所有項目的物品和價格。我想抓住3個價格最高的物品的總和。MySQL選擇與LIMIT結果的總和
我想也許SELECT SUM(items.price) FROM items ORDER BY items.price LIMIT 3
但這似乎並沒有伎倆。這可能嗎?謝謝!
我有一個表格,其中包含所有項目的物品和價格。我想抓住3個價格最高的物品的總和。MySQL選擇與LIMIT結果的總和
我想也許SELECT SUM(items.price) FROM items ORDER BY items.price LIMIT 3
但這似乎並沒有伎倆。這可能嗎?謝謝!
select sum(price) from (select items.price from items order by items.price desc limit 3) as subt;
只需使用一個子選擇:
select sum(prices) from (SELECT SUM(items.price) FROM items ORDER BY items.price LIMIT 3) as prices
LIMIT
影響查詢返回的行數和SUM只返回一個。基於this thread你可能會想嘗試:
SELECT sum(price)
FROM (SELECT price
FROM items
ORDER BY price DESC
LIMIT 3
) AS subquery;
使用子查詢或類似的東西。只是一個想法,因爲我沒有測試過實際的查詢。
SELECT SUM(items.price) from (select price FROM items ORDER BY items.price LIMIT 3)
我還沒有測試過這個,我只是寫在我的記憶。你可以嘗試這樣的:
SELECT * AS total FROM items ORDER BY price DESC
SELECT SUM(price) FROM total LIMIT 3;
編輯:我只是它似乎當我寫'SUM(items.price)`查詢失敗,但離開了桌子,即慢
正確的答案。 SUM(價格)'查詢工作 – PotatoFro 2012-03-26 19:04:15