I have the following table.
Owner_id Owner
-----------------
1 Bill
2 Steve
Animal Owner_fk_id
-----------------------
Cat 1
Dog 1
Lion 2
Car Owner_fk_id
-----------------------
Ferrari 1
BMW 1
Lotus 2
I want to list all animals and cars for the owner with at least one Ferrari. Since Bill has a Ferrari, we will list all of Bill's cars and animals.
If I use the following SQL:
SELECT *
FROM Owner
LEFT JOIN Animal ON (Animal.Owner_fk_id = Owner.Owner_id )
LEFT JOIN Car ON (Car.Owner_fk_id = Owner.Owner_id )
WHERE Car.Car = 'Ferrari'
I will receive
Owner Animal Car
---------------------------
Bill Cat Ferrari
Bill Dog Ferrari
What is my desired result:
Owner Animal Car
---------------------------
Bill Cat Ferrari
Bill Cat BMW
Bill Dog Ferrari
Bill Dog BMW
Can I find out which correct SQL statement should I use?
source
share