2012-11-29 54 views
-3

分组我有如下表总和多列在MySQL

color  shape   material  value qt 
---------------------------------------------------- 
green  cylinder  plastic   12  2 
white  cube   wood   5  1 
green  cylinder  plastic   6  2 
white  cube   wood   5  2 
green  cylinder  plastic   2  2 

我想有两个具有相同的标准值的总和。我想要下面的东西。

color  shape   material  sum 
----------------------------------------------- 
green  cylinder  plastic  28 
white  cube   wood   15 
green  cylinder  plastic  12 

我有很多颜色,形状和材料,所以我想动态查询。我不知道从哪里开始。

编辑

还有其他的特点,使每一个记录不同

+0

你是如何计算这些价值ES? –

+0

sum(value * qt) – outellou

+2

绿色圆柱体的第一个和第三个区别? – jcho360

回答

0
select color, shape, material, sum(value * qt) 
from tbl 
group by color, shape, material 
1

如果它作为你的描述使得简单听起来

SELECT color, shape, material, SUM(value) 
FROM SomeTable 
GROUP BY color, shape, material 

,但不知道你的榜样产出表的关系与你的描述和原始数据