组合两个表没有重复的结果

时间:2016-01-29 16:21:09

标签: sql oracle database-administration sysdba

我正在尝试从特定用户的两个不同表中获取角色和权限。我的疑问是:

tbl_order_item

我结束了这个:

orderItems

当用户没有多个角色/权限时,这很好,但对于像SYS这样的用户,我最终会得到orderItems

有没有办法让查询结果更好?我错过了加入吗?

PS:我正在研究oracle 11g。

如果你能帮助改善它,请这样做

先谢谢!

1 个答案:

答案 0 :(得分:0)

也许你想要union all而不是加入:

Select r.grantee, r.granted_role as role_or_privilege
From dba_role_privs r, 
Where r.grantee = (select username from dba_users where username = 'HR'); 
union all
select s.grantee, s.privilege 
from dba_sys_privs s 
Where s.grantee = (select username from dba_users where username = 'HR'); 

我将where中的逻辑与查询中的逻辑相同。但是,这对我来说更有意义:

Select r.grantee, r.granted_role as role_or_privilege
From dba_role_privs r, 
Where r.grantee = 'HR'; 
union all
select s.grantee, s.privilege 
from dba_sys_privs s 
Where s.grantee = 'HR';