Code:
SELECT
Categories.cat_id
, Categories.parent_id
FROM categories Categories
LEFT OUTER JOIN user_categories UserCategories
ON Categories.cat_id = UserCategories.cat_id
LEFT OUTER JOIN role_categories RoleCategories
ON Categories.cat_id = RoleCategories.cat_id
WHERE UserCategories.user_id = 11111
AND RoleCategories.role_id = 91919
I've formatted your query a bit, because 1-line-queries are impossible to read and understand.
In your query you use left joins, and then put criteria on them in the WHERE clause, which makes them act like INNER joins. If you really want to have a LEFT join, put the where clauses in the respective ON clauses.
And if you only want results where the join actually returns a row from the second table, use an INNER join.
Code:
SELECT
Categories.cat_id
, Categories.parent_id
FROM categories Categories
INNER JOIN role_categories RoleCategories
ON Categories.cat_id = RoleCategories.cat_id
LEFT OUTER JOIN user_categories UserCategories
ON Categories.cat_id = UserCategories.cat_id
AND UserCategories.user_id = 11111
WHERE RoleCategories.role_id = 91919
I don't understand what you are trying to accomplish though. If you don't care about the usercategories (you don't select a column from that table, and you don't care if the cat_id is present in that table) then why are you joining it?
Bookmarks