简体   繁体   中英

MySql Query to sum the rows and get the max value

I have a table product with sample data as below

product  1   2  2   3   1          
Quantity 10 15  20  10  5 

I need to findout which product has highest quantity after summing up all the available quantity for all the products

Example: Here answer would be 15+20 = 35 is greatest so product 2 has more quantity

please help with query

select product, sum(quantity) as sum_quantity
from products
group by product
order by sum_quantity desc
limit 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM