有没有办法在同一查询中引用 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
之外,还有其他编码方法吗?
最佳答案
您不能在 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
关于mysql - 引用 mySQL 查询组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768250/