2017-04-13 166 views
0

我有两个表格;下面根据条件从另一个表中选择特定记录

enter image description here

enter image description here

其结构被赋予在我分配基于菜单权利的第一个表角色ID。在某些情况下,我想覆盖特定用户(用户ID)的一些其他权利。所以这第二个表格正在用于特定用户的额外权利。 在下面的例子中; RoleID = 2和MenuID = 4没有添加权限;所以我在第二个表中添加了1行。在获取记录时,我需要从第二个表中获取该记录,而不是第一个表中的第三个记录。如何为此目的进行查询?

+1

您可以包括和格式化文本,而不是图像样本数据?你还可以向我们展示查询的预期结果吗? –

+1

你应该添加查询,直到现在, – etsa

+1

所以你需要'role_menu_rights'中的所有记录,除了那些'userid'中有'user_role_menu_rights'条目的'roleid'。为此,您需要'user_role_menu_rights'中的记录(userid,roleid)组合?清楚地给你预期的输出 – Utsav

回答

2

如果我明白你的问题,这就是你需要的。

select r.application,r.roleid,r.menuid 
case when u.roleid is null and u.menuid is null 
    then r.isadd else u.isadd end 
,case when u.roleid is null and u.menuid is null 
    then r.isedit else u.isedit end 
-- same case conditions for other columns. 

from role_menu_rights r 
left join 
user_role_menu_rights u 
on r.roleid=u.roleid 
and r.menuid=u.menuid 
+0

谢谢你它正常工作 – Pradees

0
Select u1.Application,u1.UserID, u1.RoleID, u1.MenuID, u1.isAdd, u1.isEdit, u1.isDelete, u1.IsView 
from User_Role_Menu_Right u1 
where u1.UserID = :userID 
union 
Select r.Application,u.UserID, r.RoleID, r.MenuID, r.isAdd, r.isEdit, isDelete, IsView 
from User_Role_Menu_Right u, Role_Menu_Right r 
where 
u.UserId = :userId and u.RoleId != r.RoleID and u.MenuID != r.MenuID; 
0

你可以使用union:

select * 
from role_menu_rights 
where not exists 
     (select * 
     from user_role_menu_rights 
     where RoleID = role_menu_rights.RoleID AND MenuID = role_menu_rights.MenuID) 
union all 

select Application, RoleID, MenuID, IsAdd, IsEdit, IsEdit, IsDelete, IsView 
from user_role_menu_rights 
相关问题