如何在数组中 SELECT privilege_type?
How to SELECT privilege_type in an ARRAY?
所以我想把一些值放在一起,因为它们有相同的受赠人。
但不幸的是,我找不到正确的查询。
这是基本查询(因此,它可以工作,但由于 "privilege_type",行数太多):
SELECT
table_schema,
table_name,
grantee,
privilege_type
FROM
information_schema.role_table_grants
WHERE
table_schema='public';
结果:
https://image.noelshack.com/fichiers/2019/36/5/1567753951-capture.png
所以现在我只想按 "grantee" 排成一行。所以我需要将 "privilege_type" 的所有值放入每个 "grantee".
的数组中
我已经试过了,但没用:
SELECT
table_schema,
table_name,
grantee,
ARRAY(SELECT privilege_type
FROM
information_schema.role_table_grants
GROUP BY grantee) as privileges
FROM
information_schema.role_table_grants
WHERE
table_schema='public';
我该怎么做...?
所以我只能得到(例如上一张图片)2行......
实在是看不懂...
您可以使用 group by
和 array_agg()
SELECT table_schema,
table_name,
grantee,
array_agg(privilege_type::text) as privileges
FROM information_schema.role_table_grants
WHERE table_schema='public'
GROUP BY table_schema, table_name, grantee;
所以我想把一些值放在一起,因为它们有相同的受赠人。 但不幸的是,我找不到正确的查询。
这是基本查询(因此,它可以工作,但由于 "privilege_type",行数太多):
SELECT
table_schema,
table_name,
grantee,
privilege_type
FROM
information_schema.role_table_grants
WHERE
table_schema='public';
结果: https://image.noelshack.com/fichiers/2019/36/5/1567753951-capture.png
所以现在我只想按 "grantee" 排成一行。所以我需要将 "privilege_type" 的所有值放入每个 "grantee".
的数组中我已经试过了,但没用:
SELECT
table_schema,
table_name,
grantee,
ARRAY(SELECT privilege_type
FROM
information_schema.role_table_grants
GROUP BY grantee) as privileges
FROM
information_schema.role_table_grants
WHERE
table_schema='public';
我该怎么做...? 所以我只能得到(例如上一张图片)2行...... 实在是看不懂...
您可以使用 group by
和 array_agg()
SELECT table_schema,
table_name,
grantee,
array_agg(privilege_type::text) as privileges
FROM information_schema.role_table_grants
WHERE table_schema='public'
GROUP BY table_schema, table_name, grantee;