I’m writing a Facebook app. When a user adds the application i insert a record in a users table with its user_id and name. When the user removes this app i delete the row that i created for this user. This way i have in my users table all the users that are currently using my application. Somewhere on the application page i display this list of users ordered by last time a user accessed the application (i have a datetime filed that i update)
Currently i display all users ordered by last activity.
What i would like to be able to do is display this list ordered by last activity but to show first the users that are friends with current user and after them show the rest of the users.
So for example if i have 3 users:
user_id | name | last_activity
------------------------------------
1 | Foo | 09.06.2011 09:12:00
------------------------------------
2 | Bar | 09.06.2011 09:11:00
------------------------------------
3 | Dude | 09.06.2011 09:10:00
So if current user id is 3 and he is friend with 2 then i would want my list to be like this (excluding current user):
- Bar
- Foo
Because even if Foo has more recent activity than Bar, Bar has precedence because is friend with current user.
So any idea what is the best way how i should store these friendship relations between users , how to update them when new user joins the app(i.e. a user 4 who is friend with 3 joins the application) and then hot to get the whole users list but ordered by this criteria?
Thanks.
to put all that in one query would be tough, certainly possible though.
but I would suggest you SELECT all the friends ids.
then you SELECT every user OREDERed BY last_activity and put a constraint in the WHERE-part stating NOT IN ([friends ids]).