背景
我有一个组表、用户表和一个group_members表。
groups { group_ varchar(50) not null, etc... }
users { user_id varchar(50) not null, etc... }
group_members { group_ varchar(50 not null, member varchar(50) not null }
我的要求表明一个组可以有其他组作为成员。用户需要被视为他们所属的任何组的所有组的成员。
例如,考虑这些表中的以下数据:
group_members | groups | users |
========================== | ============== | ======== |
group_ member | group_ | user_id |
-------------------------- | -------------- | -------- |
'SYSTEM_ADMIN' 'OE_ADMIN' | 'SYSTEM_ADMIN' | 'USER |
'SYSTEM_ADMIN' 'AR_ADMIN' | 'OE_ADMIN' | |
'SYSTEM_ADMIN' 'USER' | 'AR_ADMIN' | |
问我想要的结果是 什么组是“用户”的成员?应该
member
==============
'SYSTEM_ADMIN'
'OE_ADMIN'
'AR_ADMIN'
问题
我已经构建了以下查询并为我提供了所需的结果,但它看起来有点复杂。
WITH GM
AS (
SELECT GROUP_, MEMBER FROM group_members
WHERE member IN (SELECT group_ FROM groups)
)
SELECT group_ FROM group_members WHERE member = 'USER'
UNION
SELECT MEMBER AS GROUP_ FROM GM
WHERE group_ in (SELECT group_ FROM group_members WHERE member = 'USER')
有关如何使此查询更简单或更简洁的任何建议?