【发布时间】:2017-12-08 18:20:03
【问题描述】:
我希望这个 postgres 函数能够工作:
CREATE OR REPLACE FUNCTION difference_of_match_ids_in_match_history_and_match_results()
returns table(match_id BIGINT)
as
$$
BEGIN
return QUERY
SELECT *
FROM sports.match_history
WHERE match_id NOT IN (SELECT match_id
FROM sports.match_results);
END $$
LANGUAGE 'plpgsql';
这个独立的查询工作得很好:
SELECT *
FROM sports.match_history
WHERE match_id NOT IN (SELECT match_id FROM sports.match_results);
但是当我把它放到这个函数中并尝试像这样运行它时:
select *
from difference_of_match_ids_in_match_history_and_match_results();
我明白了:
SQL 错误 [42702]:错误:列引用“match_id”不明确
详细信息:它可以引用 PL/pgSQL 变量或表 柱子。其中:PL/pgSQL 函数 difference_of_match_ids_in_match_history_and_match_results() 第 3 行 返回查询
我已经看到其他具有相同错误的问题,他们建议命名子查询以指定您所指的列的哪个实例,但是,这些示例使用连接,并且我的查询在函数之外可以正常工作。
如果我确实需要为列命名,我将如何仅使用一个子查询来命名?
如果这不是问题,那么我假设我定义函数的方式有问题。
【问题讨论】:
标签: sql postgresql plpgsql