在PostgreSQL中,我正在尝试加入一个需要来自查询中其他地方的参数的set-returns函数。我怎样才能重写这个查询,这样就不会产生“对FROM子句条目的无效引用?”理解它,所写的查询需要LATERAL支持,Postgres没有。
drop table if exists questions;
create table questions (
id int not null primary key,
user_id int not null
);
insert into questions
select generate_series(1,1100), (random()*20000)::int;
drop table if exists users;
create table users (
id int not null primary key
);
insert into users select generate_series(1, 20000);
drop function if exists question_weightings();
create function question_weightings()
returns table (question_id int, weighting int) as $$
select questions.id, (random()*10)::int
from questions;
$$ language sql stable;
drop function if exists similarity(int);
create function similarity(weighting int)
returns table (user_id int, score int) as $$
select users.id, (random() * $1)::int
from users;
$$ language sql stable;
select questions.id, qw.weightings
from questions
join question_weightings() as qw
on qw.question_id = questions.id
join similarity(qw.weighting) as sim
on sim.user_id = questions.user_id;
我怀疑答案是在这个帖子中的某个地方: http://archives.postgresql.org/pgsql-general/2011-08/msg00482.php。 但我已经玩过CTE,子查询,OFFSET 0等的各种组合,并且已经空白了;每个组合似乎永远循环调用相似()而不是一次调用它并加入它。
答案 0 :(得分:2)
您的示例中有几个问题。
错误信息很清楚:
ERROR: invalid reference to FROM-clause entry for table "qw"
LINE 5: join similarity(qw.weighting) as sim on sim.user_id = questi...
^
HINT: There is an entry for table "qw", but it cannot be referenced from this part of the query.
但还有更多:
STABLE
来定义函数。weighting
和weightings
。可能是错别字。我将你的演示变成了可行的东西:
-- DROP SCHMEMA x CASCADE;
CREATE SCHEMA x
CREATE TABLE x.questions (id int PRIMARY KEY, user_id int NOT NULL);
INSERT INTO x.questions SELECT generate_series(1,11), (random()*20000)::int;
CREATE TABLE x.users (id int PRIMARY KEY);
INSERT INTO x.users SELECT generate_series(1, 200);
CREATE FUNCTION x.question_weighting()
RETURNS TABLE (question_id int, weighting int) AS
$BODY$
SELECT q.id, (random()*10)::int
FROM x.questions q;
$BODY$
LANGUAGE sql;
CREATE FUNCTION x.similarity(int)
RETURNS TABLE (user_id int, score int) AS
$BODY$
SELECT u.id, (random() * $1)::int
FROM x.users u;
$BODY$
LANGUAGE sql;
WITH qqw AS (
SELECT q.id, q.user_id, qw.weighting
FROM x.questions q
JOIN x.question_weighting() qw ON qw.question_id = q.id
-- WHERE ??
)
SELECT id, weighting
FROM qqw
JOIN (
SELECT *
FROM x.similarity((
SELECT weighting
FROM qqw
-- WHERE ??
LIMIT 1
))
) sim USING (user_id);
也许这可以在较低的层次上进行简化。