其实我的查询是这样的:
SELECT ABS(20-80) columnA , ABS(10-70) columnB ,
ABS(30-70) columnC , ABS(40-70) columnD , etc..
pb是每个ABS()实际上都是一些复杂的计算,我需要添加一个最后一列.Total巫婆是每个ABS()的SUM,我想在没有重新计算的情况下以一种方式做到这一点所有。我想要实现的目标是:
SELECT ABS(20-80) columnA , ABS(10-70) columnB ,
ABS(30-70) columnC , ABS(40-70) columnD , SUM(columnA+columnB+columnC+columnD) columnTotal
。预期结果如下:
columnA columnB columnC columnD columnTotal
60 60 40 30 190
不知道是否可能
答案 0 :(得分:2)
是的,在MySQL中你可以这样做:
SELECT
@a:=ABS(40-90) AS column1,
@b:=ABS(50-10) AS column2,
@c:=ABS(100-40) AS column3,
@a+@b+@c as columnTotal;
+---------+---------+---------+-------------+ | column1 | column2 | column3 | columnTotal | +---------+---------+---------+-------------+ | 50 | 40 | 60 | 150 | +---------+---------+---------+-------------+ 1 row in set (0.00 sec)
答案 1 :(得分:0)
你可以将它包装在另外一层中,如下所示:
select columnA, columnnB, columnnC, columnnD, (columnA+ columnnB+ columnnC+ columnnD) total
from
(
SELECT ABS(20-80) columnA , ABS(10-70) columnB ,
ABS(30-70) columnC , ABS(40-70) columnD , etc..
)
答案 2 :(得分:0)
SELECT t.*, (columnA+columnB...) as total
FROM (
SELECT ABS(20-80) columnA , ABS(10-70) columnB, ABS(30-70) columnC , ABS(40-70) columnD
...
) as t
答案 3 :(得分:0)
使用派生表:
SELECT
columnA , columnB ,
columnC , columnD ,
SUM(columnA+columnB+columnC+columnD) columnTotal
FROM
(
SELECT
ABS(20-80) columnA , ABS(10-70) columnB ,
ABS(30-70) columnC , ABS(40-70) columnD
FROM ...
) AS dt