简体   繁体   中英

How to sum values in MySQL query

I know how to count total results:

SELECT COUNT(*) as Count FROM store_items

This will give me the total number of unique items in my store. However, some items may have more than 1 quantity. The table is like:

ID    Sku   Qty
1     500   3
2     501   1
3     502   6

How do I modify the query to return 10 instead of 3 (counting the values in all the Qty fields)?

You should group them:

SELECT SUM(`Qty`) as `Quantity` 
FROM `store_items`
GROUP BY `Sku`

This way you get sums for each Sku you have. So if you have multiple rows of each Sku, they would all be summed.

SELECT SUM(Qty) AS sum FROM store_items

mysql:总和

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