mysql查询时有个字段是变量,我可以将MySQL查询中的表达式重新用作另一个字段的变量吗?...

Is there any workaround so I can actually do something like this without having to repeat the entire expression or force a UNION or temporary table?

SELECT (complex expression) AS variable1,

(complex expression based on variable1) AS variable2

Since variable1 is not defined and available to the 2nd item because of how mysql works, the above concept can never work.

I either have to repeat the expression for variable2, or use a UNION or a temporary table and use two passes.

Is there some trick that I am not aware of to accomplish this more efficiently?

(note that I need to know the answer for both variable1 and variable2 as they are then used for an INSERT)

Thanks for any ideas!

解决方案

push the first calculation in to a derived table:

select variable1

, complex_function(variable1, other_column) as variable2

, yet_another column

from (select complex_operation as variable1

, other_column

, yet_another_column

from whatever) dt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值