I’m having trouble wrapping my head around how to write this query.
A hypothetical problem that is that same as the one I’m trying to solve:
Say I have a table of apples. Each apple has numerous attributes, such as color_id, variety_id and the orchard_id they were picked from.
The color_id, variety_id, and orchard_id all refer to their respective tables: colors, varieties, and orchards.
Now, say I need to query for all apples that have color_id = '3', which refers to yellow in the colors table.
I want to somehow obtain this yellow value from the query.
Make sense?
Here’s what I was trying:
SELECT * FROM apples, colors.id WHERE color_id = '3'
LEFT JOIN colors ON apples.color_id = colors.id
This is how you do the join in general. It will return all apples whether or not a color is specified:
If you only want yellow apples, it should probably be an inner join, since you are requiring that a.color_id not be null in your where clause:
Update: