MySQL Tables
MySQL: Plants
+----+--------+
| id | plant |
+----+--------+
| 1 | tree |
+----+--------+
| 2 | flower |
+----+--------+
| 3 | tree |
+----+--------+
MySQL: Trees
+----+------+
| id | type |
+----+------+
| 1 | hard |
+----+------+
| 3 | soft |
+----+------+
What I’m trying to do
I want to select all rows in the table plants.
But if plant = tree, then the type in the table trees must be hard in order for it to be displayed.
So, the example above should output: 1=tree and 2=flower.
3=tree should be excluded because its type = soft.
What I’ve tried so far…but didn’t work
a) I guess I cannot use union, because the columns are different.
b) I already tried left join, but that didn’t work either:
select p.id, p.plant
from plants AS p
left join
(
select `id`, `type`
from `trees`
) AS t ON p.id = t.id
WHERE t.type = 'hard'
any ideas on how I can do it?
yes
I hope this works. Anyway the problem with your current query is not the left join (that is ok actually), but that you specify the tree type must be hard so you eliminate the rows with null tree that the left join gave you.