I need to find all User
s that belong to a certain Country
(ID = 4321
). But a User
‘s Location
can have…
- EITHER a
City
- OR a
Cityaddition
, as you can see in…
The database schema:
User -< Locations >- City >- District >- Region >- Country
>- Cityaddition >- District >- Region >- Country
Legend: (read from left to right)
-< : "...has many..."
>- : "...has one..."
How do I have to modify my query below in order to make this EITHER–OR condition work in 1 single query?
(Currently, this query only selects User
s that have BOTH a City
AND a Cityaddition
, which is never the case in reality.)
SELECT users.* FROM users
INNER JOIN locations ON locations.user_id = users.id
INNER JOIN cities ON cities.id = locations.city_id
INNER JOIN districts ON districts.id = cities.district_id
INNER JOIN regions ON regions.id = districts.region_id
INNER JOIN cityadditions ON cityadditions.id = locations.cityaddition_id
INNER JOIN districts
districts_cityadditions ON districts_cityadditions.id = cityadditions.district_id
INNER JOIN regions
regions_districts ON regions_districts.id = districts_cityadditions.region_id
WHERE (regions.country_id = 4321)
2
Answers
As a single query, you can do based on a parallel hierarchy and doing a logical NOT NULL on either side. Two separate via would be better as Barmar indicates due to the explicit OR.
Also, use aliases to shorten and make more readable and you can remove all those
ticks
. You typically only need those when dealing with column names that are also reserved words.Using
UNION
, but minimising code repetition, and the number of redundant joins…