I have list of sections stored in database in table Sections with columns (sectionId, sectionTypeId, sectionName) and privileges in table UserPrivilages with columns (userPrivilagesId, userId, sectionTypeId).
I want to select all sections from table Sections but mark those sectionTypes by sectionTypeId stored in UserPrivilages by userId.
Something like:
SELECT sectionId, sectionTypeId, sectionName, (true/false) as privilage
FROM Sections
If I JOIN this with table UserPrivilages I get results only exist in both tables, but I want to have also Sections that user dont haveprivilages` for.
This true/false is from UserPrivilages table if sectionTypeId from Sections table exists in UserPrivilages table by userId than return true, else false
So result would be for example
SectionId sectionTypeId sectionName privilage
1 1 Name1 true or 1
2 2 Name2 false or 0
It sounds like you want a left join, and possibly a
COALESCEto substitute an answer when no row exists in privilages:Which will have a
0wherever no match exists.Or, possibly, re-reading the question, you’re not selecting a column from
p, you just want to check for existence:(Assuming
userPrivilagesIdis a non-NULL column inp).