I have the following location hierarchy. Jobs are assigned to locations. If I have just the location name how can I return all jobs in that location and in any place that comes under that location?
E.g if I select Leeds
or Oakwood
then only jobs 1 and 2 should be returned. If I select Yorkshire
or England
or Uk
or Europe
then all 3 jobs would be returned.
Locations:
id | name | continent | country | admin1 | admin2 | city
-------------------------------------------------------------------------------------
1 | Europe | | | | |
2 | UK | Europe | | | |
3 | England | Europe | UK | | |
4 | Yorkshire | Europe | UK | England | |
5 | Leeds | Europe | UK | England | Yorkshire |
6 | Oakwood | Europe | UK | England | Yorkshire | Leeds
Jobs:
id | location_id
--------------------
1 | 6
2 | 6
3 | 4
This is straight forward when you know which column to filter by e.g
Select jobs.*
from jobs
INNER JOIN locations on locations.id = jobs.location_id
where locations.name = 'Europe' OR location.continent = 'Europe'
Select jobs.*
from jobs
INNER JOIN locations on locations.id = jobs.location_id
where locations.name = 'UK' OR location.country = 'UK'
But how can you achieve the same when you don't know which column to filter in.