MySQL计算为行

时间:2017-11-10 05:48:24

标签: mysql sql

考虑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 

给出一列但我需要它是一行

1 个答案:

答案 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

enter image description here

Demo