简体   繁体   English

获取好友之间的用户position

[英]Get users position between friends

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...我试图在用户朋友之间获得用户 position,但我不知道如何做到这一点......

I have two tables.我有两张桌子。

Table 1: friends (where all the users friends are listed)表 1:好友(列出所有用户好友)

Table 2: users (where all the users are listed)表 2:用户(列出所有用户)

I want the query to check the users position between his friends.我希望查询检查他的朋友之间的用户 position。

So if I, for example have ID 1 (with 100 credits) and a friend with ID 2 (with 21 credits), the query would list my position as 1.因此,例如,如果我的 ID 为 1(有 100 个学分)和一个朋友的 ID 为 2(有 21 个学分),则查询会将我的 position 列为 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:首先,我假设您的users表至少有这些列:

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.为了获得 position,我建议为此使用 PHP 而不要尝试将其全部放在一个查询中。 So get a sorted list like Flimzy described and get the position by using an array function like array_search.因此,通过使用数组 function (如 array_search)获得一个像 Flimzy 描述的排序列表并获得 position。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM