postgresql - 如何在 from 和 where 子句中使用相同的 psql 子查询?
问题描述
我想在 from 和 where 子句中使用相同的子查询。尝试了两种方法,但在两种情况下在查询的不同位置都得到了相同的错误。
问题 1:
select * from (subquery1) as t_feature where id = (select MAX(id) from t_feature);
问题 2:
select * from t_feature where id = (select MAX(id) from (subquery1) as t_feature);
错误:
ERROR: relation "t_feature" does not exist
对于临时灵魂,我为子查询创建了一个视图,并用它来代替子查询。但我不想为这种情况创建视图。
解决方案
使用公用表表达式:
with t_feature as (
...
)
select *
from t_feature
where id = (select MAX(id) from t_feature);
推荐阅读
- amazon-web-services - 如何为 Route53 -> EC2 -> API 端点反向代理路径设置 HTTP 测试
- javascript - 添加网页视图 2 次
- reactjs - 功能组件错误:解析错误:Unexpected token, expected ","
- html - 如何将 SQL Server 查询转换为 JSON
- r - 无法抓取中央银行 BR 的网站(在 R 中)
- python-tesseract - 训练 Tesseract 为图标添加标签
- c# - 如何在 Visual Studio C# 中对系列图表进行分组和命名
- c# - 对于 LINQ 谓词的副作用,我们能保证什么?
- java - 在java中将时间戳转换为纪元
- html - 如何从任何现有网页的检查元素中仅选择 HTML 和/或 CSS 代码?