Assuming three tables which map a many to many relationship.
People
1 Bob
2 Mark
3 Peter
4 Tracy
5 Robert
6 ...
Favorite_Food
1 Icecream
2 Fish&Chips
3 Chocolate
4 Pizza
Pople_To_Favorite_Food
1, 2 # Bob likes Fish&Chips.
2, 2 # Mark likes Fish&Chips too.
4, 2
3, 3
3, 2
1, 1 # Bob likes Icevream.
3, 1
4, 1
5, 3
What is the recommended way to realize the following query:
- Selecting every favorite food of Bob.
- Then select every person, who shares at least one similar favorite food with Bob.
- List the names found.
So what I am looking for is some kind of back reference.
In this example it would have to result in: Bob likes Icecream and Fish&Chipy; Mark, Peter and Tracy like at least one food which Bob likes. So Mark, Peter and Tracy are listed.
I thought of doing something like
SELECT fk_people_id FROM [...join the tables here...] WHERE favorit_food_id IN (
SELECT fk_favorite_food_id [...join the tables here...])
Is this the way to go, or is there a more suitable concept I should be using?
Perhaps I should mention, that the table I want to query is really huge (Millions of entrys).
Try this one –