I have a sales_cat table and a user_cat table.
sales_cat
user_cat
I need to get all sales_cat rows connected to the user_cat table for a specific user, indicating whether this user has a category or not. For example, for id_user = 4 it should return:
id_cat | name | selected
1 | kids | 1
2 | men | 1
3 | women | 0
Of course, the "selected" field is actually a value that depends on the existence of a related record in user_cat. I set the table structure in sqlfiddle .
My current solution returns only related data:
SELECT sales_cat.id_cat, sales_cat.name
FROM sales_cat
LEFT JOIN user_cat ON user_cat.id_cat = sales_cat.id_cat
WHERE user_cat.id_user = 4
... which returns:
id_cat | name
1 | kids
2 | men
I'm still missing the "selected" column and 3 | women .
Any ideas? Thank!