我想知道是否可以在mysql中执行类似的操作:
select 5 as a, 1 as b, a + b as c;
问题是因为我有这个:
CONVERT( SUM(ifnull(commission,0.00)) ,DECIMAL(6,2)) as 'commission',
CONVERT( SUM(ifnull(income,0.00)) ,DECIMAL(6,2)) as 'income',
CONVERT( (SUM(ifnull(income,0.00)) - SUM(ifnull(commission,0.00))) ,DECIMAL(6,2)) as 'profit'
当然,如果不再需要将所有值全部变为SUM,那么第3行会更加优化。
答案 0 :(得分:3)
你做不到
select 5 as a, 1 as b, a + b as c;
但你可以做到
select a, b, a + b as c from (select 5 as a, 1 as b) as whatever;
(这是一个嵌套查询,当从外部查询中使用时,它充当假表。)
将这些应用到您的案例中,我们有:
select comission, income, income - comission from (
select
CONVERT( SUM(ifnull(commission,0.00)) ,DECIMAL(6,2)) as 'commission',
CONVERT( SUM(ifnull(income,0.00)) ,DECIMAL(6,2)) as 'income'
FROM ...
) as t
希望这有帮助。
答案 1 :(得分:2)
如果查询没有GROUP BY子句,则可以使用变量 -
SELECT @a:=SUM(1), @b:=SUM(2), @c:=@a + @b FROM table
此外,子查询可以在这里使用。
答案 2 :(得分:0)
select a, b, a+b as c
from (
select 5 as a, 1 as b
) t