I got 3 tables that i need to get data from:
RECIPE - id, name, worktime, date, describtion
TAGS - id, name
INGREDIENT - id, name, created
Now, both tags and ingredient has a many-to-many relation with recipe.
When i try to query my DB to generate a result, i get way too many results.
SELECT *
FROM (`recipe` r, `tag` t, `ingredient` i)
JOIN `recipe_has_tag` rt ON `rt`.`tag_t_id` = `t`.`t_id`
JOIN `ingredient_has_recipe` ir ON `ir`.`ingredient_in_id` = `i`.`in_id`
WHERE `r`.`r_id` = ir.recipe_r_id
AND r.r_id = rt.recipe_r_id
AND r.r_id = ir.recipe_r_id
AND r.r_id = 1
I this particular example, i am looking to get 1 recipe, 4 tags and 4 ingredients.
The problem is, instead of returning 4 rows, it mixes the ingredients with the tags, returning 16 results instead of the expected 4.
Right now i end up with 16 results where the each tag has a row with an ingredient, making it 16 results.
Getting something like this (very simplified)
R.name = good dish, i.name = potato, t.name = "potato dish"
R.name = good dish, i.name = carrot, t.name = "potato dish"
R.name = good dish, i.name = potato, t.name = "carrot dish"
R.name = good dish, i.name = carrot, t.name = "carrot dish"
What i want is rows where each row contains 1 tag and 1 ingredient, like this
R.name = good dish, i.name = potato, t.name = "potato dish"
R.name = good dish, i.name = carrot, t.name = "carrot dish"
And a fiddle for good measure, showing a one line but with too many results in the tags and ingredient column