-1

我有以下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
4

3 回答 3

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 ║
╚════════════╩════════════╩═════════════════════╝
于 2013-05-04T04:20:16.780 回答
2

你需要group by aggregate function with count and sum.

尝试这个。

select   product_id, count(product_id), sum(selling_price)
from     tablename
group by product_id;
于 2013-05-04T04:25:12.697 回答
0

试一试,未在本地测试

SELECT  product_id,
        COUNT(product_id),
        SUM(Amount)
FROM    yourTable
GROUP BY    product_id
于 2013-05-04T04:36:06.993 回答