-3
 Master Table
nID  Elementtype elementval 
1    volume         5          
1    rating         +          
1    volume         3          
1    rating         -          
2    volume         2          
2    rating         *            
2    volume         4         
2    rating         +          
3    volume         3          
3    rating         + 

client      table
  nID       client   
   1        halkins    
   2        narnia
   3        avatar
   4        narnia   
   5        newsco
   6        halkins 

如何在客户方面获得三种类型的平均音量和评分总和,例如对于 halkins

client     No(sum)       volumeavg      ngve   potve    neutral
 halkins    4                 4          1       1         0

如何在同一个查询中获得其他值基本上是 volumeavg 或者是他们的任何其他方法 体积是体积的平均值,对于 halkins 5+3/2=4

4

1 回答 1

3

您需要加入表格,然后对结果进行分组

SELECT   client,
         COUNT(*) AS `No(sum)`,
         AVG(IF(Elementtype='volume', elementval, NULL)) AS volumeavg,
         SUM(Elementtype='rating' AND elementval='-') AS ngve,
         SUM(Elementtype='rating' AND elementval='+') AS potve,
         SUM(Elementtype='rating' AND elementval='*') AS neutral
FROM     Master JOIN client USING (nID)
GROUP BY nID

sqlfiddle上查看。

于 2012-08-30T13:07:19.500 回答