2017-10-06 89 views
0

我有以下表格:選擇沒有其他表的關係記錄

enter image description here

,其中每個表的主鍵是它的名稱+編號(例如:UserRole.UserRoleId是主鍵對於UserRole,UserRole.RoleIdRole的外鍵在UserRole)。

所以我想選擇所有Funcionality沒有Profil(Funcionality.ProfileFuncionality.Count = 0),我用下面的查詢做:

SELECT * FROM Funcionality WHERE FuncionalityId NOT IN (SELECT FuncionalityId FROM ProfileFuncionality);

但我無法弄清楚如何做以下選擇:

所有Funcionality沒有Role

Funcionality.ProfileFuncionality.Count > 0 
AND Funcionality.ProfileFuncionality.Profile.RoleProfile.Count = 0 

所有Funcionality沒有User

Funcionality.ProfileFuncionality.Count > 0 
AND Funcionality.ProfileFuncionality.Profile. RoleProfile.Count > 0 
AND Funcionality.ProfileFuncionality.Profile.RoleProfile.Role.UserRole.Count = 0 

回答

1

只是擴展子選擇:

所以對於沒有一個角色的所有功能:爲所有的funcionality

SELECT * 
FROM Funcionality 
WHERE FuncionalityId NOT IN 
    (SELECT FuncionalityId 
    FROM ProfileFuncionality 
    JOIN Profile 
     ON Profile.Profileid = ProfileFuncionality.Profileid 
    JOIN RoleProfile 
     ON Profile.Profileid = RoleProfile.Profileid); 

沒有用戶:

SELECT * 
FROM Funcionality 
WHERE FuncionalityId NOT IN 
    (SELECT FuncionalityId 
    FROM ProfileFuncionality 
    JOIN Profile 
     ON Profile.Profileid = ProfileFuncionality.Profileid 
    JOIN RoleProfile 
     ON Profile.Profileid = RoleProfile.Profileid 
    JOIN Role 
     ON Role.Roleid = RoleProfile.Roleid 
    JOIN UserRole 
     ON Role.Roleid = UserRole.Roleid) 
MINUS 
SELECT * 
FROM Funcionality WHERE FuncionalityId NOT IN 
    (SELECT FuncionalityId 
     FROM ProfileFuncionality 
     JOIN Profile 
     ON Profile.Profileid = ProfileFuncionality.Profileid 
     JOIN RoleProfile 
     ON Profile.Profileid = RoleProfile.Profileid); 

我使用減號沒有一個角色出現減去功能,因爲他們顯然還沒有用戶

+0

謝謝你,它的工作,只爲第一查詢ü忘了補充一點: '和FONCTIONNALITEID(從PROFILFONCTIONNALITE中選擇不同的FONCTIONNALITEID);',我已經更新了您的答案 –

相關問題