I have a MySQL table with the following schema:
+---------+---------+----------------------+------------+
| User ID | Song ID | Recommending User ID | Created On |
+---------+---------+----------------------+------------+
| 1001 | 54 | 1004 | 2011-07-21 |
| 1002 | 23 | 1005 | 2011-07-28 |
| 1002 | 166 | 1001 | 2011-07-31 |
+---------+---------+----------------------+------------+
What I’m trying to do is give users points when someone favorites a song that they recommend. So the query takes every user in the system and finds out who has the most points in relation to the user its scanning.
So, say we take user 1001. 1001 favorited song id 54 that was recommended by 1004 on 7/21. The query needs to give 40 points when scanning 1001.
Now what makes this complicated is that I want to add a second level. So for each user that 1001 favorited (i.e. 1004) I want to search them (1004) the same way giving anyone 1004 favorited 20 points when still computing 1001.
Just to clarify once more. Let’s take 1002 as an example:
Computing 1002:
User 1005 gets 40 pts
User 1001 gets 40 pts
Computing 1005
nothing
Computing 1001
User 1004 gets 20 pts
Done
Any help on how to start is greatly appreciated 🙂
*Edit: specified mysql
How about this (I’m calling the table you outlined above “Recommendations”):
Each time a song is marked “favorite,” we need to look whether someone else recommended that. So suppose user
UserIDjust markedSongIDas his “favorite.” The following person gets 40 points:What you asked is how to check for another level of recommendations. We can do it by a
JOINof the Recommendations table to itself:The
LEFT JOINmakes sure that if the 20-point user doesn’t exist, it doesn’t break the query; you’ll just getNULLfor Twenty.Recommending_user_id.Note that you could join again to give 10 points to the third level up, if you wanted. 🙂