【问题标题】:How to get all user list group by location in postgres sequelize?如何在postgres sequelize中按位置获取所有用户列表组?
【发布时间】:2021-07-31 03:34:20
【问题描述】:
username location
user1 L1
user2 L2
user3 L2
user4 L3
user5 L4

我需要最终输出

L1 - ['user1']

L2 - ['user2', 'user3']

L3 - ['user4']

L4 - ['user5']

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    你可以在 postgres 中使用array_agg

    select location , array_agg(username)
    from table
    group by location
    

    如果你想要逗号分隔的字符串而不是列表,你也可以使用string_agg(username,',')

    db小提琴here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多