【问题标题】:How to SELECT privilege_type in an ARRAY?如何在 ARRAY 中选择 privilege_type?
【发布时间】:2020-01-09 01:32:10
【问题描述】:

所以我想把一些价值观放在一起,因为它们有相同的受让人。 但不幸的是,我找不到正确的查询。

这是基本查询(所以,它有效,但由于“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

所以现在我只想通过“受让人”制作一行。因此,我需要将“privilege_type”的所有值放入每个“被授权人”的 ARRAY 中。

我试过了,但是不行:

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行...... 实在看不懂……

【问题讨论】:

    标签: sql arrays postgresql information-schema


    【解决方案1】:

    您可以将group byarray_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;
    

    【讨论】:

    • 您好,谢谢您的回答!但我收到了这条消息:“错误:找不到数据信息_schema.character_data SQL 状态的数组类型:42704”
    • @Ashka:啊,在 information_schema 中使用的那种愚蠢的类型。您需要将值转换为 text(请参阅我的编辑)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多