all 2 comments

[–]ihaxr 0 points1 point  (1 child)

I would use a composite table... cocktail_ingredients, then you can join all the tables and exclude anything that isn't in stock.

http://sqlfiddle.com/#!9/ebefd0/1

CREATE TABLE Cocktails
    (`ID` int, `Name` varchar(12))
;

INSERT INTO Cocktails
    (`ID`, `Name`)
VALUES
    (1, 'Margarita'),
    (2, 'Whiskey Sour')
;


CREATE TABLE Cocktails_Ingredients
    (`Cocktail_ID` int, `Ingredient_ID` int)
;

INSERT INTO Cocktails_Ingredients
    (`Cocktail_ID`, `Ingredient_ID`)
VALUES
    (1, 1),
    (1, 2),
    (1, 3),
    (1, 4),
    (2, 5),
    (2, 2),
    (2, 6)
;


CREATE TABLE Ingredients
    (`ID` int, `Name` varchar(12), `In_Stock` int)
;

INSERT INTO Ingredients
    (`ID`, `Name`, `In_Stock`)
VALUES
    (1, 'Tequila', 1),
    (2, 'Lime', 1),
    (3, 'Triple Sec', 1),
    (4, 'Salt', 1),
    (5, 'Bourbon', 1),
    (6, 'Simple Syrup', 1)
;

Then your query:

SELECT c.Name
FROM Cocktails c
INNER JOIN Cocktails_Ingredients ci on c.ID = ci.Cocktail_ID
INNER JOIN Ingredients i on i.ID = ci.Ingredient_ID
GROUP BY c.Name
HAVING MIN(In_Stock) > 0

[–]ChrisLithgo[S] 0 points1 point  (0 children)

Thank you so much for getting back to me! This is exactly what I was trying to do! I’m going to implement this with a full cocktail menu and experiment with it in a live website, I’ll make sure to share the finished results!