![]() |
[SQL]How to select based on many-to-many relationship.
I'm embarrassed to ask this, but I have limited SQL experience.
I have a many to many relationship, and I need to select rows in one table based on more than one value in the other table. For example if you have a recipe table, an ingredient table, and an intersection relation then what would you need to do select all recipes based on two or more ingredients? So the tables would be: RECIPE(RecipeID, RecipeName) INGREDIENT(IngredientID,IngredientName) RECIPEINGREDIENT(IngredientID, RecipeID) |
Wow... I just can't figure this one out O_o
Something using a subquery maybe? |
Please note this is totally untested but it seems logical to me and should be syntactically correct. Its worth a try anyway. SQL gurus reading this may have a more efficient way to do it too, i'm far from an expert. Anyway, here goes:
Code:
SELECT * Worth a try welshbyte |
That looks good. I'll try it out. I got it working using a subquery, but this may be better. It is certainly more simple and easier to understand!
|
Use EXISTS checks. Eg:
Code:
SELECT RecipeName |
Another way of doing this is to do:
Code:
SELECT RecipeName |
All times are GMT -8. The time now is 02:38 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project