Arrays 在postgres select中,将列子查询作为数组返回?

Arrays 在postgres select中,将列子查询作为数组返回?,arrays,postgresql,subquery,aggregate-functions,Arrays,Postgresql,Subquery,Aggregate Functions,以前做过,但记忆会消失,就像眼镜一样 希望从以数组形式返回的每个用户的tag.tag_ID的用户中获取select select usr_id, name, (select t.tag_id from tags t where t.usr_id = u.usr_id) as tag_arr from users u; 有了这个想法,嵌入式查询标记将是一个数组使用: 或从子查询的结果中选择: select u.usr_id, name,

以前做过,但记忆会消失,就像眼镜一样

希望从以数组形式返回的每个用户的tag.tag_ID的用户中获取select

select usr_id, name, (select t.tag_id from tags t where t.usr_id = u.usr_id) as tag_arr from users u; 有了这个想法,嵌入式查询标记将是一个数组

使用:

或从子查询的结果中选择:

select
    u.usr_id, 
    name, 
    array(
        select tag_id 
        from tags t 
        where t.usr_id = u.usr_id
        ) as tag_arr
from users u

第二个选项简单快捷,而第一个选项更通用,当您需要从相关表中获取多个聚合时,尤其方便。

@a_horse_with_no_name-yep。太多的语言,太多年了-谢谢
select
    u.usr_id, 
    name, 
    array(
        select tag_id 
        from tags t 
        where t.usr_id = u.usr_id
        ) as tag_arr
from users u