Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 08-19-2004, 04:36 PM   #1 (permalink)
Upright
 
[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)
jakal is offline  
Old 08-19-2004, 05:08 PM   #2 (permalink)
Fluxing wildly...
 
MrFlux's Avatar
 
Location: Auckland, New Zealand
Wow... I just can't figure this one out O_o

Something using a subquery maybe?
__________________
flux (n.)
Medicine. The discharge of large quantities of fluid material from the body, especially the discharge of watery feces from the intestines.
MrFlux is offline  
Old 08-20-2004, 08:11 AM   #3 (permalink)
Insane
 
Location: Wales, UK, Europe, Earth, Milky Way, Universe
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 *
FROM RECIPE
WHERE RECIPEINGREDIENT.RecipeID = RECIPE.RecipeID
AND RECIPEINGREDIENT.IngredientID = INGREDIENT.IngredientID
AND (INGREDIENT.IngredientName LIKE 'Eggs' OR INGREDIENT.IngredientName LIKE 'Bacon');
I've used 'Eggs' and 'Bacon' as examples here but more ingredients would be added by extending with more OR's between the parentheses.

Worth a try

welshbyte
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte
welshbyte is offline  
Old 08-20-2004, 03:24 PM   #4 (permalink)
Upright
 
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!
jakal is offline  
Old 08-20-2004, 08:14 PM   #5 (permalink)
Tilted
 
Use EXISTS checks. Eg:

Code:
SELECT RecipeName
FROM Recipe
WHERE EXISTS (
     SELECT *
     FROM RecipeIngredient
     WHERE RecipeIngredient.RecipeID = Recipe.RecipeID
     AND RecipeIngredit.IngredientID = <Ingredient1ID>
) AND EXISTS (
     SELECT *
     FROM RecipeIngredient
     WHERE RecipeIngredient.RecipeID = Recipe.RecipeID
     AND RecipeIngredit.IngredientID = <Ingredient2ID>
) ...etc
Welshbyte's answer will return recipes that match only one ingredient.
magua is offline  
Old 08-23-2004, 03:12 AM   #6 (permalink)
Upright
 
Location: Melbourne, Australia
Another way of doing this is to do:

Code:
SELECT RecipeName
FROM Recipe
INNER JOIN RecipeIngredient AS Ingredient1 ON
     (Ingredient1.RecipeID = Recipe.RecipeID)
INNER JOIN RecipeIngredient AS Ingredient2 ON
     (Ingredient2.RecipeID = Recipe.RecipeID)
WHERE
     (Ingredient1.IngredientID = <IngredientID1>)
     AND (Ingredient2.IngredientID = <IngredientID2>)
mino is offline  
 

Tags
based, manytomany, relationship, select, sqlhow


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:19 PM.

Tilted Forum Project

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73