This is my current query – its not getting the required result. I want it do display all of the “resources” even if they dont have a connection.
SELECT *
FROM (`user_permissions`)
JOIN `user_groups` ON `user_groups`.`id` = `user_permissions`.`role`
JOIN `user_resources` ON `user_resources`.`id` = `user_permissions`.`resource`
WHERE `role` = '4'
When I try left join or right join it still returns the same result. The result I get is:
id | role | resource | name
5 | 4 | 2 | Changelog
I want
id | role | resource | name
5 | 4 | 2 | Changelog
null | null | null | Resource2
null | null | null | Resource3
Is this possible?
Looking through your query,
roleis part ofuser_permissionswhich is one of the connections that may or may not exist. Consider changing your where clause toWHERE `role`= '4' OR `role` IS NULLif you want to display these null records as well…Also, although this can be accomplished by a right join, I believe it’s more readable/understandable if you select from
user_resourcesinstead, then left join on the other tables. This follows from your problem description statement; you want to “display all of the ‘resources’ even if they don’t have a connection”, which means you want to select from resources, then join on any connections if they exist.