Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- recipes_tbl
- | id | Recipe | Directions |
- ingredients_tbl
- | id | Ingrediant | Category |
- recipe_to_ingredient
- | id | id_recipe | id_ingredient | Quantity
- SELECT R.ID, R.Recipe, R.Directions
- FROM Ingredients_tbl I
- INNER JOIN recipe_to_ingredient RI ON I.id = RI.id_ingredient
- INNER JOIN recipes_tbl R ON R.id = R.id_recipe
- WHERE I.ID IN (1 ,2)
- GROUP BY R.ID, R.Recipe, R.Directions
- HAVING COUNT(*) > 1
- SELECT
- id, Recipe, Directions
- FROM
- recipes_tbl AS r
- WHERE EXISTS
- ( SELECT *
- FROM recipe_to_ingredient AS ri
- JOIN ingredients_tbl AS i
- ON i.id = ri.id_ingredient
- WHERE ri.id_recipe = r.id
- AND i.Ingredient = 'beef'
- )
- AND EXISTS
- ( SELECT *
- FROM recipe_to_ingredient AS ri
- JOIN ingredients_tbl AS i
- ON i.id = ri.id_ingredient
- WHERE ri.id_recipe = r.id
- AND i.Ingredient = 'potatoes'
- )
- ...
- SELECT
- r.id, r.Recipe, r.Directions
- FROM
- recipes_tbl AS r
- JOIN
- recipe_to_ingredient AS ri1
- ON ri1.id_recipe = r.id
- JOIN
- ingredients_tbl AS i1
- ON i1.id = ri1.id_ingredient
- AND i1.Ingredient = 'beef'
- JOIN
- recipe_to_ingredient AS ri2
- ON ri2.id_recipe = r.id
- JOIN
- ingredients_tbl AS i2
- ON i2.id = ri1.id_ingredient
- AND i2.Ingredient = 'potatoes'
- ...
- SELECT
- r.id, r.Recipe, r.Directions
- FROM
- recipes_tbl AS r
- JOIN
- recipe_to_ingredient AS ri
- ON ri.id_recipe = r.id
- JOIN
- ingredients_tbl AS i
- ON i.id = ri.id_ingredient
- WHERE i.Ingredient IN ('beef', 'potatoes', ...)
- GROUP BY r.id
- HAVING COUNT(*) = @n --- number of items in above list
- SELECT R.ID, R.Recipe, R.Directions
- FROM recipes_tbl R
- WHERE R.ID IN (SELECT id_recipe
- FROM recipe_to_ingredient RI
- INNER JOIN Ingredient I ON I.id = RI.id_ingredient
- WHERE I.Ingredient in ('Beef','Potato'))
- SELECT
- r.Recipe
- FROM
- recipes_tbl r
- JOIN recipe_to_ingredient r_i
- ON r.id = r_i.id_recipe
- JOIN ingredients_tbl i
- ON i.id = r_i.id_ingredient
- GROUP BY
- r.Recipe
- HAVING
- COUNT(IF(i.Ingrediant = 'beef', 1, 0)) > 0
- AND
- COUNT(IF(i.Ingrediant = 'potatoes', 1, 0)) > 0
Add Comment
Please, Sign In to add comment