考虑MySQL数据库中的表
id | A | B | C
1 | 10 | 20 | 30
2 | 13 | 32 | 44
3 | 34 | 24 | 92
如何计算生成类似
的内容compute | A | B | C
sum | 57 | 76 | 166
average |
通常
sum(A) as total
给出一列但我需要它是一行
答案 0 :(得分:6)
结合两个查询,一个查找总和,另一个查找平均值:
SELECT 'sum' AS compute, SUM(A) AS A, SUM(B) AS B, SUM(C) AS C FROM yourTable
UNION ALL
SELECT 'average', AVG(A), AVG(B), AVG(C) FROM yourTable