【发布时间】:2019-01-28 11:51:55
【问题描述】:
如何多次使用子查询的结果?有没有办法命名该结果并在其他地方使用它?我知道 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;
【问题讨论】:
-
称为CTE(公用表表达式)。也许如果你发布你的尝试,我们可以告诉你为什么没有成功。
-
您成为会员的时间已经足够长,知道我们需要更多,对吧?代码、尝试、错误等。此外,如果您链接某些内容,请包含/引用重要位,以便当链接失效时,问题不会。 - 编辑:并可能使用pg_fetch_all 并将子查询结果保存为独立查询?
-
@rkeet 可以吗?
-
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;
标签: postgresql