Skip to content Skip to sidebar Skip to footer

Find A Value Which Contains In All Rows Of A Table

I need to select all values which are contained in ALL rows of a table. I have table “Ingredient” and ProductIngredient(there I have a recipe of a product). Ingredient | ing

Solution 1:

This will give you all Ingredients from I that are in PI for each product. This is assuming that each product does not have multiple rows for a product and ingredient combination.

SELECT I.Ingredient_Id 
FROM Ingredients I INNER JOIN ProductIngredient PI
     ON PI.Ingredient_Id = I.Ingredient_Id
GROUP BY I.Ingredient_Id
HAVING COUNT(*) >= (SELECT COUNT(DISTINCT Product_id) FROM ProductIngredient)

Post a Comment for "Find A Value Which Contains In All Rows Of A Table"