让许多用户属于多个群组。
编写一个功能,创建一个包含name
和用户列表的组。
答案 0 :(得分:-1)
CREATE FUNCTION groups_create(varchar(50), bigint[]) RETURNS SETOF bigint AS
$$
WITH g AS (
INSERT INTO groups (name)
VALUES ($1)
RETURNING id
), gm AS (
INSERT INTO group_members
SELECT id, unnest($2) FROM g
)
SELECT id FROM g
$$
LANGUAGE SQL;