reusing alias in SELECT

后端 未结 3 1835
长情又很酷
长情又很酷 2021-01-05 07:24

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

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

3条回答
  •  醉梦人生
    2021-01-05 07:57

    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;
    

提交回复
热议问题