开发者

Get users position between friends

开发者 https://www.devze.com 2023-03-19 13:29 出处:网络
I\'m trying to get a users position between the users friends, but I don\'t have any idea of how I can do this...

I'm trying to get a users position between the users friends, but I don't have any idea of how I can do this...

I have two tables.

Table 1: friends (where all the users friends are listed)

Table 2: users (where all the users are listed)

I want the query to check the users position between his friends.

So if I, for example have ID 1 (with 100 开发者_C百科credits) and a friend with ID 2 (with 21 credits), the query would list my position as 1.


You don't really provide much information on your table layout, so it's going to be impossible for me to provide a very specific example. I'm also afraid I don't really understand your question, but I'll give it a shot...

First, I'll assume your users table has at least these columns:

id (PK)
credits

And that the friends table has these columns:

user (FK to users.id)
friend (FK to users.id)

Now, if I understand your question, you want to rank all of a user's friends, based on how many credits they have, so:

SELECT u.id,u.credits
FROM friends AS f
JOIN users AS us ON f.friend = u.id
WHERE f.user = 1
ORDER BY u.credits DESC;


in order to get the position I would recommend using PHP for this and not try to put it all in one query. So get a sorted list like Flimzy described and get the position by using an array function like array_search.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号