I have 3 tables:
T_User
UserId
Name
T_Role
RoleId
Name
T_UsersRoles
Id
FK_RoleId
FK_UserId
I want to have all records from T_Role. Nevertheless i want to pass specific UserId. Even if user doesn't have relation with some records from T_Role i want to list all T_Rule records and on the right just make value 1 if there is relation and 0 if it's not
This is my current query:
SELECT role.RoleId, role.[Name], CASE WHEN usersroles.ID IS NULL THEN 0 ELSE 1 END
FROM T_Role userrole
LEFT JOIN T_UsersRoles usersroles ON userrole.ID = usersroles.FK_RuleID
WHERE usersroles.FK_UserID = 30;
Nevertheless i only get those records where there is relation. I thought using LEFT join i do it but it's not
so instead of getting:
1 RoleA 1
2 RoleB 0
3 RoleC 0
i got:
1 RoleA 1
because User = 30 got only one relation. How to make it as above?