MySQL - Define a variable within select and use it within the same select

前端 未结 3 762
长情又很酷
长情又很酷 2020-11-27 18:47

Is there a possibility to do something like this?

SELECT 
    @z:=SUM(item),
    2*@z
FROM
    TableA;

I always get NULL for the second col

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 19:11

    Works in mysql 5.5

    select @code:=sum(2), 2*@code
    
    +---------------+---------+
    | @code:=sum(2) | 2*@code |
    +---------------+---------+
    |             2 |       4 |
    +---------------+---------+
    

提交回复
热议问题