Skip to content
Advertisement

Select data from three tables only if one of them has reference to parent table

I have three tables person, his cars, his houses. And i need select persons with his cars and houses if one of childs(car, house) table have reference to parent(person).

I tried with join, but don’t know how use OR in this condition.

and result should be

Advertisement

Answer

You can left join twice and ensure that one of the joins succeded. The rest is aggregation:

Demo on DB Fiddle:

name   | cities          | brands      
:----- | :-------------- | :-----------
Mark   | Chicago Moscow  | null        
David  | Boston New York | toyota volvo
Andrew | null            | bmw opel    
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement