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
答案 0 :(得分:3)
您需要join表格,然后分组结果:
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上查看。