Created
August 5, 2011 22:38
-
-
Save kevinpeno/1128700 to your computer and use it in GitHub Desktop.
SQL to get all recipes that contain all ingredients the user has
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE VIEW view_recipe_by_ingredient AS | |
SELECT R.*, RI.liquorID -- Replace R.* with columns in recipes table | |
FROM recipes R | |
JOIN recipeIngredients RI ON RI.recipeID = R.id; | |
CREATE VIEW view_recipes_in_cabinet AS | |
SELECT L.userID, RbI.id, COUNT( RbI.liquorID ) as LiquorCount, RbI.* -- Replace RbI.* with remaining columns in view_recipe_by_ingredient | |
FROM view_recipe_by_ingredient RbI | |
LEFT JOIN liquorCabinet L USING( liquorID ); | |
CREATE VIEW view_cabinet_count AS | |
SELECT userId, COUNT( liquorId ) as total | |
FROM liquorCabinet; | |
CREATE VIEW view_recipes_in_cabinet_using_all_ingredients AS | |
SELECT RiC.name | |
FROM view_recipes_in_cabinet RiC | |
LEFT JOIN view_cabinet_count CC USING( userID ) | |
WHERE RiC.LiquorCount = CC.total; | |
-- Select recipes in user's cabinet, ordered by number of ingredients DESC | |
SELECT * | |
FROM view_recipes_in_cabinet | |
WHERE userID = ? | |
ORDER BY LiquorCount DESC; | |
-- Select recipes in user's cabinet that use all ingredients in cabinet | |
SELECT * | |
FROM view_recipes_in_cabinet_using_all_ingredients | |
WHERE userID = ?; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment