I am trying to solve this problem:
“Our data:
We have three Tabels in the database :
Plant – describes the possible plants
Planted – whatever the gardener planted
Picked – the crop that was provided after the plantation."
What I am trying to do is join to 3 tables like that:
SELECT plant.PlantID, plant.name, planted.Seeds, picked.Amount
FROM plant
INNER JOIN planted ON plant.PlantID = planted.PlantFK
INNER JOIN picked ON picked.PlantFK = plant.PlantID
So I could get data about specific PlantID how many seeds was planted and how many was picked.
But for some reason Im getting multiple plantids.
Because the database has more then 1 entry about the plant/ picked about the same PlantID..
How can I fix that join?
Just use an aggregate function
SUMorCOUNTwith aGROUP BY plant.PlantID, plant.namelike so:SQL Fiddle Demo
If you want to get the plants that have no seeds, and never picked from, you have to use
LEFT JOINwithIFNULL()to get zeros instead ofNULLs like so:Updated SQL Fiddle Demo