有没有办法在同一个查询中引用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
之外,有没有办法对此进行编码?
答案 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