引用mySQL查询组件

时间:2015-06-10 22:15:27

标签: mysql sql select alias

有没有办法在同一个查询中引用MySQL查询的部分内容?

例如:

SELECT 50000 AS starting_principle, .065*50000 AS interest, 
    principle + interest AS principle_plus_interest

查询集中的第三列principle_plus_interest给出了错误。除了编写50000 + .065*50000 AS principle_plus_interest之外,有没有办法对此进行编码?

1 个答案:

答案 0 :(得分:3)

您无法在select列表(或where子句中引用别名)。解决此问题的一种方法是使用子查询:

SELECT starting_principle, 
       interest, 
       principle + interest AS principle_plus_interest 
FROM   (SELECT 50000 AS starting_principle, .065*50000 AS interest
        FROM   some_table) t
相关问题