0

我正在创建一个如下所示的存储过程:

CREATE FUNCTION get_components(_given_user_id integer) RETURNS TABLE (id integer, name    varchar, active boolean, parent integer, type smallint, description varchar, email varchar, user_id integer, component_id integer, flag smallint) AS $body$
BEGIN
RETURN QUERY SELECT s.* FROM st_components s LEFT JOIN (SELECT a.* FROM st_users_components_perms a WHERE a.user_id=_given_user_id) ON a.component_id=s.id ORDER BY name;
END;
$body$ LANGUAGE plpgsql;

问题是, ON a.component_id=s.id ORDER BY name 不起作用,因为此时 a.component_id 超出范围。有没有办法在查询之外将“a”声明为 st_users_components_perms?这是如何解决的?非常感谢您的任何见解!

4

1 回答 1

0
SELECT
    s.*
FROM
    st_components s
LEFT JOIN
    (SELECT
        a.*
    FROM
        st_users_components_perms a
  WHERE
      a.user_id = <CONSTANT>
    ) AS x
ON
    x.component_id = s.id
ORDER BY
    name;

尽管查询似乎毫无意义,但让我们假设它是您真实查询的简化版本。

于 2012-07-02T18:01:07.610 回答