如何在1个查询中选择sum()多列?

时间:2013-09-27 07:30:20

标签: mysql sql

只是样本表:Cus_fi。

enter image description here

我可以吗

SELECT SUM(dollar,euro,pound) FROM cus_fi ; in 1 query.

按年龄范围显示结果。

   20 - 25   1500    410     166
   26 - 30   5300    2000    584

感谢学生真正想要的帮助。

2 个答案:

答案 0 :(得分:3)

select
    round(age / 5) * 5 - 4,
    round(age / 5) * 5,
    SUM(dollar),
    SUM(euro)
FROM TEST
GROUP BY round(age / 5)

您可以使用更多列扩展它。见DEMO:http://sqlfiddle.com/#!2/160bb/15

答案 1 :(得分:1)

试试这个:

SELECT age, SUM(dollar) AS Total_Dollar, SUM(Euro) AS Total_Euro, 
SUM(Pound) AS Total_Pound 
    FROM cus_fi GROUP BY age

编辑:

SELECT CONCAT(2*floor(age/2), '-', 2*floor(age/2) + 2) as `age_range`, SUM(dollar) AS Total_Dollar, SUM(Euro) AS Total_Euro, 
SUM(Pound) AS Total_Pound 
    FROM cus_fi GROUP BY 1 ORDER BY age