Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 在psql中运行并引用函数一次_Postgresql - Fatal编程技术网

Postgresql 在psql中运行并引用函数一次

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,查询变得更加复杂(对匹配节

我有一个函数,用于返回复杂节点查找的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,查询变得更加复杂(对
匹配节点的调用越来越多)

您可以使用

假设函数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);