Guest User

Untitled

a guest
Jul 20th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. recipes_tbl
  2. | id | Recipe | Directions |
  3.  
  4. ingredients_tbl
  5. | id | Ingrediant | Category |
  6.  
  7. recipe_to_ingredient
  8. | id | id_recipe | id_ingredient | Quantity
  9.  
  10. SELECT R.ID, R.Recipe, R.Directions
  11. FROM Ingredients_tbl I
  12. INNER JOIN recipe_to_ingredient RI ON I.id = RI.id_ingredient
  13. INNER JOIN recipes_tbl R ON R.id = R.id_recipe
  14. WHERE I.ID IN (1 ,2)
  15. GROUP BY R.ID, R.Recipe, R.Directions
  16. HAVING COUNT(*) > 1
  17.  
  18. SELECT
  19. id, Recipe, Directions
  20. FROM
  21. recipes_tbl AS r
  22. WHERE EXISTS
  23. ( SELECT *
  24. FROM recipe_to_ingredient AS ri
  25. JOIN ingredients_tbl AS i
  26. ON i.id = ri.id_ingredient
  27. WHERE ri.id_recipe = r.id
  28. AND i.Ingredient = 'beef'
  29. )
  30. AND EXISTS
  31. ( SELECT *
  32. FROM recipe_to_ingredient AS ri
  33. JOIN ingredients_tbl AS i
  34. ON i.id = ri.id_ingredient
  35. WHERE ri.id_recipe = r.id
  36. AND i.Ingredient = 'potatoes'
  37. )
  38. ...
  39.  
  40. SELECT
  41. r.id, r.Recipe, r.Directions
  42. FROM
  43. recipes_tbl AS r
  44. JOIN
  45. recipe_to_ingredient AS ri1
  46. ON ri1.id_recipe = r.id
  47. JOIN
  48. ingredients_tbl AS i1
  49. ON i1.id = ri1.id_ingredient
  50. AND i1.Ingredient = 'beef'
  51. JOIN
  52. recipe_to_ingredient AS ri2
  53. ON ri2.id_recipe = r.id
  54. JOIN
  55. ingredients_tbl AS i2
  56. ON i2.id = ri1.id_ingredient
  57. AND i2.Ingredient = 'potatoes'
  58. ...
  59.  
  60. SELECT
  61. r.id, r.Recipe, r.Directions
  62. FROM
  63. recipes_tbl AS r
  64. JOIN
  65. recipe_to_ingredient AS ri
  66. ON ri.id_recipe = r.id
  67. JOIN
  68. ingredients_tbl AS i
  69. ON i.id = ri.id_ingredient
  70. WHERE i.Ingredient IN ('beef', 'potatoes', ...)
  71. GROUP BY r.id
  72. HAVING COUNT(*) = @n --- number of items in above list
  73.  
  74. SELECT R.ID, R.Recipe, R.Directions
  75. FROM recipes_tbl R
  76. WHERE R.ID IN (SELECT id_recipe
  77. FROM recipe_to_ingredient RI
  78. INNER JOIN Ingredient I ON I.id = RI.id_ingredient
  79. WHERE I.Ingredient in ('Beef','Potato'))
  80.  
  81. SELECT
  82. r.Recipe
  83. FROM
  84. recipes_tbl r
  85. JOIN recipe_to_ingredient r_i
  86. ON r.id = r_i.id_recipe
  87. JOIN ingredients_tbl i
  88. ON i.id = r_i.id_ingredient
  89. GROUP BY
  90. r.Recipe
  91. HAVING
  92. COUNT(IF(i.Ingrediant = 'beef', 1, 0)) > 0
  93. AND
  94. COUNT(IF(i.Ingrediant = 'potatoes', 1, 0)) > 0
Add Comment
Please, Sign In to add comment