在SELECT中重复使用别名 [英] reusing alias in SELECT

查看:137
本文介绍了在SELECT中重复使用别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是添加另一个列计算(cr - dr)

What I am trying to do is add another column that calculates (cr - dr)

看到你不能在SELECT子句中重复使用别名,

Seeing as you cannot re-use an alias inside a SELECT clause, how would you go about calculatin total

    SELECT SUM(b.bet_win * cy.fx_rate )as dr, SUM(b.bet_loss * cy.fx_rate ) as cr, cr+dr as total
    FROM ....
    WHERE ....


推荐答案

在SQL Server或Oracle中,我会使用CTE,使用MySQL,您将使用一个子查询:

In SQL Server or Oracle, I'd use a CTE, but since you're using MySQL, you'd use a subquery:

SELECT dr, cr, cr + dr as total 
FROM (
    SELECT 
         SUM(b.bet_win * cy.fx_rate ) as dr, 
         SUM(b.bet_loss * cy.fx_rate ) as cr
    FROM ....
    WHERE ....) t;

这篇关于在SELECT中重复使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆