gpt4 book ai didi

postgresql - 从子查询中保存值并在以后重用?

转载 作者:行者123 更新时间:2023-11-29 13:11:15 24 4
gpt4 key购买 nike

如何多次使用子查询的结果?有没有办法命名该结果并在其他地方使用它?我知道 with xyz as ... 但这似乎行不通?

我找到了 this并想要更具体的东西?

损坏的代码示例:

with g_surf as (select surface_area from countries where name like 'Germa%')
select abs(surface_area - g_surf) from countries;

使用整个子查询的工作代码:

select abs(surface_area - (select surface_area from
countries where name like 'Germa%')) from countries;

最佳答案

只是将问题标记为已解决:
g_surf,在您的示例中,是 CTE(通用 表达式),即它充当表,而不是字段。

with g_surf as (select surface_area from countries where name like 'Germa%')
select abs(surface_area) from g_surf;

当然,如果你的countries表中有一个g_surf字段,你可以这样写:

with myCTE as (select surface_area, g_surf from countries where name like 'Germa%')
select abs(surface_area - g_surf) from myCTE;

关于 CTE 的更多信息 here .

关于postgresql - 从子查询中保存值并在以后重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com