0

执行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;
4

3 回答 3

2

您可以使用变量来执行此操作,例如

select @m:=2*2 as m, @m*2 as n

-你可能想n稍后使用(实际上,那将是@n你将能够使用,但不能n直接使用),那么它将是

select @m:=2*2 as m, @n:=@m*2 as n
于 2013-10-10T13:12:05.143 回答
2

您可以使用子查询来做到这一点:

select m, (m*2) as n
from
(
  select 2*2 as m
) sub_query
于 2013-10-10T13:13:37.490 回答
1

编辑: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;
于 2013-10-10T13:07:30.057 回答