MySQL选择与LIMIT结果的总和
问题描述:
我有一个表格,其中包含所有项目的物品和价格。我想抓住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