madi madi - 3 years ago 72
MySQL Question

Advice me on the algorithm to match people

Currently i am working on a project where i need to match people based on the categories of food they like:

This is the scenario:

I have a list of USERS and their favorite foods in my database. The database structure is as follows:

USERS(id,name,email,gender,dob)
Fav_Food (id,user_name,food,desc)


Data for users table:

1, Alice, alice@lala.com, female, 11 Oct 2010

2, Bob, bob@lala.com, male, 12 Oct 2010

3, Jason, jason@lala.com, male, 13 Oct 2010

Data for fav_foods table:

1, Alice, apple, some desc

2, Alice, banana, some desc

3, Alice, Pear, some desc

4, Bob, apple, some desc

5, Bob, custard cake, some desc

6, Jason, banana,some desc

6,Jason,apple,some desc

Imagine that i am Alice where i like apple,banana & pear. How would i be able to match people based on the favorite food? For example, i first check if anyone likes apple,banana and pear (inclusive of all three) and than go with the permutation of only two combination
(apple,banana)(apple,pear)(banana,pear)(banana,apple)
....and so on.....

Imagine it like a Venn diagram where the interaction is what i am interested. I am interested to suggest users with the most matched. Is there any algorithm available that i can use for php?

Answer Source

what you could do is join the fav_foods table to itself and then count the matches:

SELECT u2.user_name,count(*) as likeness 
FROM `fav_food` as u1 INNER JOIN `fav_food` as u2 ON (u1.user_name = 'alice' AND 
                                                 u1.food = u2.food AND 
                                                 u2.user_name != "alice")
GROUP BY u2.user_name 
ORDER BY likeness DESC

it will output:

user_name   likeness
jason       2
bob         1

the trick is on the conditions of the inner join... =)

Hope this helps

EDITED: oops i corrected the query =)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download