执行select 2*2 as 'm', (m*2) as 'n'
退货
/* SQL Error (1054): Unknown column 'm' in 'field list' */
有没有办法在列子句中使用列的别名?
还:
select sum(x) as sum, sum*2 as doubleSum from myTable;
您可以使用变量来执行此操作,例如
select @m:=2*2 as m, @m*2 as n
-你可能想n
稍后使用(实际上,那将是@n
你将能够使用,但不能n
直接使用),那么它将是
select @m:=2*2 as m, @n:=@m*2 as n
您可以使用子查询来做到这一点:
select m, (m*2) as n
from
(
select 2*2 as m
) sub_query
编辑:OP编辑的问题
如果需要参数,请使用以下语法
select @m:=2 as m, @m+1 as n
select @m:=2*2 as 'm', (@m*2) as 'n'
也适用于现实生活中的例子
select @sum:=sum(impression) as sum, (@sum)*2 as doubleSum from kpi_funnel;