我有一个看起来像这样的mysql查询:
SELECT
SUM(reg_yr) AS reg_yr_total,
SUM(spot_as_reg_yr) AS spot_as_reg_yr_total
FROM foo
WHERE bar
GROUP BY baz
ORDER BY reg_yr_total
效果很好。如果我想将ORDER BY子句更改为reg_yr_total+spot_as_reg_yr_total
,我会收到一条错误,指出Reference 'reg_yr_total' not supported (reference to group function)
。
为什么我可以单独使用这些列中的每一列,但是一旦我尝试将它们加在一起就会失败?有办法解决这个问题吗?
答案 0 :(得分:1)
如果您不想选择其他列,请尝试以下操作:
SELECT
SUM(reg_yr) AS reg_yr_total,
SUM(spot_as_reg_yr) AS spot_as_reg_yr_total
FROM foo
WHERE bar
GROUP BY baz
ORDER BY SUM(reg_yr) + SUM(spot_as_reg_yr)
答案 1 :(得分:0)
尝试将它们汇总到另一个虚拟列:
SELECT
SUM(reg_yr) AS reg_yr_total,
SUM(spot_as_reg_yr) AS spot_as_reg_yr_total,
(reg_yr_total + spot_as_reg_yr_total) AS reg_yr_total
FROM foo
WHERE bar
GROUP BY baz
ORDER BY reg_yr_total
这是未经测试的,但应该有用。
如果这是一个错误的答案,请告诉我,我很乐意将其删除。