PostgreSQL:使用 Select 的 return 作为参数调用 psql 函数

PostgreSQL : Call a psql function with the return of a Select as a parameter

我用 create function xxx(id uuid) returns void as $$ 创建了一个函数。 现在,我想用 select xxx(select id from mytable where ...); 调用它,但这不起作用。我该怎么做?

只需处理 ID 并将其来源留在函数调用之外:

select xxx(id)
from mytable where ...;

针对反对者的更新:查看实际效果 here

您不能将子查询作为参数传递。一种方法是使用 LATERAL JOIN:

SELECT xxx(s.id)
FROM table t1
LEFT JOIN LATERAL (select id from mytable where ... order by ... limit 1) s
  ON true;

如果您需要在主 table 和子查询之间做一些关联,这很有用。


如果只有一个值:

SELECT xxx(s.id)
FROM (subquery) s;

或在下面回答。