我尝试在这个查询中使用Postgres的别名,但Postgres停止并抱怨错误:列"subtotal"不存在
SELECT SUM(price) AS subtotal,
subtotal * 3.0 AS fees,
(subtotal + fees) AS total
FROM cart
您不能将别名作为下一列的一部分使用吗?
我尝试在这个查询中使用Postgres的别名,但Postgres停止并抱怨错误:列"subtotal"不存在
SELECT SUM(price) AS subtotal,
subtotal * 3.0 AS fees,
(subtotal + fees) AS total
FROM cart
不可以在同一条SQL语句中重复使用列别名 - 使用以下方法:
SELECT SUM(t.price) AS subtotal,
SUM(t.price) * 3.0 AS fees,
SUM(t.price + fees) AS total
FROM CART t
您可以在ORDER BY子句中引用列别名,一些数据库也支持在GROUP BY和HAVING子句中进行引用。
SELECT SUM(t.price) AS subtotal,
SUM(t.price) * 3.0 AS fees,
SUM(t.price + fees) AS total
FROM CART t
SELECT SUM(t.price) AS subtotal,
SUM(t.price) * 3.0 AS fees,
SUM(t.price + (t.price * 3.0)) AS total
FROM CART t