Mysql - 相同产品的总和值

时间:2013-12-16 11:37:28

标签: mysql

我想对此有一些想法:

product_id   || tag_id || weight
--------------------------------
4            ||  7     || 5
3            ||  7     || 5
3            ||  7     || 5
3            ||  6     || 3
2            ||  4     || 2
2            ||  6     || 3
1            ||  4     || 2

我需要这样的东西: product_x总重量= Y,使用Mysql。

行动应该是:

if product_id[rowN] == product_id[rowN+1]
then sum(weight[rowN] + weight[rowN+1]

我尝试过google-ing但找不到任何有用的东西。

由于

2 个答案:

答案 0 :(得分:2)

GROUP BY clause automatic group all same product ids and sum these weight.

请阅读此内容以获取更多信息http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

试试这个,

SELECT SUM(weight),product_id from tablename group by product_id 

答案 1 :(得分:0)

select product_id, sum(weight) FROM table GROUP BY product_id