PostgreSQL - 错误列参考" id"很暧昧

时间:2017-08-17 14:54:11

标签: postgresql

我已将所有内容别名,但似乎无法在没有

的情况下运行此查询
  

错误列引用\" id \"是不明确的"

如果我删除其中一个联接似乎有效,但我只是对为什么它不能与两个人合作感到困惑?

create function influence.person_info(user_id integer)
  returns setof influence.person_object as
$$
    declare
    obj influence.person_object;
    begin
        select t1.email as a_email, t2.organisation_url as a_org, t3.first_name as a_first, t3.last_name as a_last into obj
        from influence_private.person_account as t1
        inner join influence_private.organisation_account as t2 on (t1.organisation_id = t2.id)
        inner join influence.person as t3 on (t1.person_id = t3.id)
        where id = $1;
        return next obj;
    end;
$$ LANGUAGE plpgsql stable;

任何指针?

3 个答案:

答案 0 :(得分:0)

如果您查看查询:

select t1.email as a_email, ...
from influence_private.person_account as t1
join influence_private.organisation_account as t2 
  on (t1.organisation_id = t2.id)   -- here
join influence.person as t3 
  on (t1.person_id = t3.id)         -- here

您的表格(influence_privateinfluence)都有名为id的列。 Postgresql不知道你要使用哪一个,所以你必须使用像t2.id这样的全名。

答案 1 :(得分:0)

where 子句中,您需要明确说明您所指的 id

答案 2 :(得分:0)

您的查询问题是t2t3两个表都有一个名为id的列。在Where条款中id是不明确的,因为它不知道您引用的idt2t3)指定了它,它会正常工作

示例修复:

create function influence.person_info(user_id integer)
  returns setof influence.person_object as
$$
    declare
    obj influence.person_object;
    begin
        select t1.email as a_email, t2.organisation_url as a_org, t3.first_name as a_first, t3.last_name as a_last into obj
        from influence_private.person_account as t1
        inner join influence_private.organisation_account as t2 on (t1.organisation_id = t2.id)
        inner join influence.person as t3 on (t1.person_id = t3.id)
        where t1.id = $1;
        return next obj;
    end;
$$ LANGUAGE plpgsql stable;