我有以下mysql表:
product_id selling_price
10 200
10 200
11 100
11 100
13 300
如何返回以下内容
product_id total total_selling_price
10 2 400
11 2 200
13 1 300
答案 0 :(得分:3)
SELECT product_ID,
COUNT(*) totalCount,
SUM(selling_price) total_selling_price
FROM TableName
GROUP BY product_ID
输出
╔════════════╦════════════╦═════════════════════╗
║ PRODUCT_ID ║ TOTALCOUNT ║ TOTAL_SELLING_PRICE ║
╠════════════╬════════════╬═════════════════════╣
║ 10 ║ 2 ║ 400 ║
║ 11 ║ 2 ║ 200 ║
║ 13 ║ 1 ║ 300 ║
╚════════════╩════════════╩═════════════════════╝
答案 1 :(得分:2)
您需要group by aggregate function with count and sum
。
试试这个。
select product_id, count(product_id), sum(selling_price)
from tablename
group by product_id;
答案 2 :(得分:0)
给它一个镜头,而不是在本地测试
SELECT product_id,
COUNT(product_id),
SUM(Amount)
FROM yourTable
GROUP BY product_id