Say I have these three tables:
Table: Baskets
id | name
1 Sale
2 Premium
3 Standard
4 Expired
Table: Fruit
id | name | basketid
1 Apples 1
2 Oranges 2
3 Grapes 3
4 Apples 2
5 Apples 4
Table: Veggies
id | name | basketid
1 Carrots 1
2 Peas 2
3 Asparagus 1
It may seem like the second two tables should have just been one table called produce, but in the real situation there is good reason for them to be different tables. I need to write a join that returns rows if the basket has rows in either the fruit or veggie tables. I thought I could accomplish this with two left joins like so:
Select Baskets.*, fruit.name as fruit,
veggies.name as veggies
from Baskets
left join Fruit on Baskets.id = Fruit.basketid
left join veggies on Baskets.id = Veggies.basketid
where Baskets.id = 2;
But this statement returns values in fields that I would like to be empty. The actual output:
id | name | fruit | veggies
2 Premium Oranges Peas
2 Premium Apples Peas
The output I would like:
id | name | fruit | veggies
2 Premium Oranges
2 Premium Apples
2 Premium Peas
How can I accomplish this?