查询以对重复的字段求和

时间:2010-06-01 21:25:14

标签: mysql

这是mysql数据

id usr good quant delayed   cart_ts 
    ------------------------------------------------------
14  4   1   1   0   20100601235348
13  4   11  1   0   20100601235345
12  4   4   1   0   20100601235335
11  4   1   1   0   20100601235051
10  4   11  1   0   20100601235051
9   4   4   1   0   20100601235051
15  4   2   1   0   20100601235350
16  4   7   1   0   20100602000537
17  4   3   1   0   20100602000610
18  4   3   1   0   20100602000616
19  4   8   1   0   20100602000802
20  4   8   1   0   20100602000806
21  4   8   1   0   20100602000828
22  4   8   1   0   20100602000828
23  4   8   1   0   20100602000828
24  4   8   1   0   20100602000828
25  4   8   1   0   20100602000828
26  4   8   1   0   20100602000829
27  4   8   1   0   20100602000829
28  4   9   1   0   20100602001045
29  4   10  1   0   20100602001046

我需要将女孩和田野中的字段分组。 good具有求和定量字段的重复值 为了获得这样的smth:

id usr good quant delayed   cart_ts 
    ------------------------------------------------------
14  4   1   2   0   20100601235348
13  4   11  2   0   20100601235345
12  4   4   2   0   20100601235335
15  4   2   1   0   20100601235350
16  4   7   1   0   20100602000537
17  4   3   2   0   20100602000610
19  4   8   9   0   20100602000802
28  4   9   1   0   20100602001045
29  4   10  1   0   20100602001046

我需要做哪些MySQL查询才能产生这种效果?

1 个答案:

答案 0 :(得分:1)

SELECT id,usr,good,SUM(quant),delayed,cart_ts FROM table GROUP BY usr,good