2012-11-29 92 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 

,但不知道你的榜樣產出表的關係與你的描述和原始數據