简体   繁体   English

如何压缩返回的具有多个角色的用户行

[英]How to condense returned rows of users with multiple roles

I'm trying to wrap my head around this logic, but simply having a tough time.我正在尝试解决这个逻辑,但只是度过了一段艰难的时光。 While trying to keep the database normalized, users can have multiple roles (example shows 2, but could have more than 2, currently have 6 total roles), and I'd like to return data to make it easily viewed on a front end UI.在尝试保持数据库规范化的同时,用户可以有多个角色(示例显示 2 个,但可能有 2 个以上,目前总共有 6 个角色),我想返回数据以便在前端 UI 上轻松查看.

Tables simplified:简化的表格:

USERS用户

id ID first_name
1 1 john约翰
2 2 sarah莎拉

ROLES角色

id ID name名称
1 1 admin行政
2 2 manager经理
3 3 scorekeeper记分员

USER_ROLE USER_ROLE

user_id用户身份 role_id role_id
1 1 1 1
2 2 2 2
2 2 3 3

Running this Postgres query will return 3 rows:运行此 Postgres 查询将返回 3 行:

SELECT roles.name, users.first_name, users.id AS uid
FROM users
  INNER JOIN user_role ON user_role.user_id = users.id
  INNER JOIN roles ON roles.id = user_role.role_id
ORDER BY users.id;

CURRENT OUTPUT电流输出

[
  { name: ‘admin’, uid: 1, first_name: 'john' },
  { name: ‘manager’, uid: 2, first_name: 'sarah' },
  { name: ‘scorekeeper’, uid: 2, first_name: 'sarah' },
] 

DESIRED OUTPUT期望的输出

[
  { uid: 1, first_name: 'john’, role_list: [‘admin’] },
  { uid: 2, first_name: 'sarah’, role_list: [‘manager’, ‘scorekeeper’] },
]

I'm looking to have my data formatted a little different - basically merging the rows on the roles.name and then adding a new “column” of the combined roles.names.我希望我的数据格式略有不同 - 基本上合并roles.name 上的行,然后添加组合的roles.names 的新“列”。 This begs the question, should I just create a “role_list” column stored as an array of strings?这就引出了一个问题,我应该创建一个存储为字符串数组的“role_list”列吗? Of course this denormalizes things, but is probably simpler to execute.当然,这会使事情非规范化,但执行起来可能更简单。

I'm also using node.js, and trying to format the CURRENT OUTPUT in javascript gets tricky when trying to loop through and "look ahead" to see if the uid has multple roles.我也在使用 node.js,并且尝试在 javascript 中格式化 CURRENT OUTPUT 在尝试循环并“向前看”以查看 uid 是否具有多个角色时变得棘手。

Any guidance would be appreciated - or even if I need to rethink my logic in the database design.任何指导将不胜感激 - 或者即使我需要重新考虑我在数据库设计中的逻辑。

You simply want aggregation:您只需要聚合:

SELECT u.id, u.first_name, ARRAY_AGG(r.name) as roles
FROM users u 
JOIN user_role ur ON u4.user_id = u.id 
JOIN roles r ON r.id = ur.role_id
GROUP BY u.id;

Note the use of table aliases.注意表别名的使用。 These make the query easier to write and to read.这些使查询更易于编写和阅读。

Insert values into a temp table.将值插入临时表。 Then use distinct Uid.然后使用不同的 Uid。

Something like this像这样的东西

Declare Table @temp(role,first_name,uid)声明表@temp(role,first_name,uid)

Insert into @temp(SELECT roles.name, users.first_name, users.id AS uid FROM users INNER JOIN user_role ON user_role.user_id = users.id INNER JOIN roles ON roles.id = user_role.role_id ORDER BY users.id)插入@temp(SELECT roles.name, users.first_name, users.id AS uid FROM users INNER JOIN user_role ON user_role.user_id = users.id INNER JOIN roles ON roles.id = user_role.role_id ORDER BY users.id)

SELECT DISTINCT UID,role,first_name From @temp SELECT DISTINCT UID,role,first_name From @temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM