postgres 中计算值的列别名
我尝试在 postgres 中使用别名来执行此查询,但 postgres 停止并抱怨错误:列“小计”不存在
SELECT SUM(price) AS subtotal,
subtotal * 3.0 AS fees,
(subtotal + fees) AS total
FROM cart
您不能使用别名作为下一列的一部分吗?
I tried to uses aliases in postgres for this query, but postgres stops and complains that ERROR: column "subtotal" does not exist
SELECT SUM(price) AS subtotal,
subtotal * 3.0 AS fees,
(subtotal + fees) AS total
FROM cart
You cannot use aliases as part of your next column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以,您不能在同一 SQL 语句中重复使用列别名 - 使用:
您可以在 ORDER BY 子句中引用列别名,某些数据库还支持在
GROUP BY
和中引用>HAVING
子句。No, you can not re-use a column alias within the same SQL statement - use:
You can reference the column alias in the ORDER BY clause, some databases also support referencing in the
GROUP BY
andHAVING
clauses.关于您的答案的问题:
如果您无法在同一 SQL 语句中重复使用列别名,那么不应该是以下情况:
Question about your answer:
Should it not be the following if you can not re-use a column alias within the same SQL statement: