引用 mySQL 个查询组件
referencing mySQL query components
有没有办法在同一查询中引用 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 查询的部分内容?
例如:
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