Two tables:
table1
id | name
1 | aaa
2 | bbb
3 | ccc
table2
id | param
1 | x
1 | z
2 | x
table1.id = table2.id
I need to select everything from table1, with field param (1 if have, 0 if no, or null) by given param.
For example, by param “x”:
result:
id | name | param
1 | aaa | 1
2 | bbb | 1
3 | ccc | 0
All I can do is just:
SELECT table1.id, table1.name, table2.param
FROM table1
LEFT JOIN table2 ON table1.id = table2.id
WHERE table2.param = 'x'
But it doesn’t select all table1.
What is the right query for this task?
You are applying the filter AFTER the join. Try this instead: