Postgresql 在psql中运行并引用函数一次
我有一个函数,用于返回复杂节点查找的ID表 使用此函数的查询需要多次-是否有方法获取并命名函数结果一次-以使代码更清晰:Postgresql 在psql中运行并引用函数一次,postgresql,Postgresql,我有一个函数,用于返回复杂节点查找的ID表 使用此函数的查询需要多次-是否有方法获取并命名函数结果一次-以使代码更清晰: SELECT channels.name FROM channels WHERE ( channels.to_id IN (SELECT matchingNodes(1,1)) AND channels.from_id IN (SELECT matchingNodes(1,1)) ); 我正在使用PostgreSQL 11,查询变得更加复杂(对匹配节
SELECT channels.name
FROM channels
WHERE (
channels.to_id IN (SELECT matchingNodes(1,1))
AND channels.from_id IN (SELECT matchingNodes(1,1))
);
我正在使用PostgreSQL 11,查询变得更加复杂(对匹配节点的调用越来越多)您可以使用
假设函数matchingnodes被定义为返回表(…)
,您还可以通过选择函数的列两次来避免第二个子查询。设置返回函数应在开头的FROM
子句中使用
因此,不要使用selectmatchingnodes(1,1)
而是使用selectid,id from matchingnodes(1,1)
(假设函数返回的列名为id
):
with nodes as (
select id, id
from matchingnodes(1,1)
)
select ch.name
from channels ch
where (ch.to_id, ch.from_id) in (select id, id,
from nodes);