[英]How to select the row with highest value from the result of an existing query?
I have an SQL query that returns a list of results. 我有一个SQL查询,它返回结果列表。 The sql is :
的SQL是:
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory
The output is something like 输出是这样的
----------------
category|qty
----------------
a | 10
b | 100
c | 554
Based on the result of the sql statement above, how do I retrieve the row with maximum quantity? 根据上述sql语句的结果,如何检索最大数量的行? (in this case I want the query to return just "c" )
(在这种情况下,我希望查询仅返回“ c”)
you can add order by + limit 1 to get this : 您可以按+限制1添加订单以获取此信息:
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory
order by qty desc limit 1
In order to fetch the highest qty, just get the first result ordered by qty like: 为了获取最高数量,只需获得第一个按数量排序的结果:
SELECT * FROM items ORDER BY qty desc limit 1
In your own case, 就您自己而言,
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory ORDER BY qty LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.