简体   繁体   English

PHP / MySQL根据引荐次数对用户进行排名并选择一条记录

[英]PHP/MySQL Rank users based on referral count and Select one record

So I currently have a table in my database called 'users'. 因此,我目前在数据库中有一个名为“用户”的表。

Lets say 'users' has two columns: 'Email' and 'ReferralCount'. 假设“用户”有两列:“电子邮件”和“ ReferralCount”。

Example: 例:

Email     ReferralCount
1@1.com   5
2@2.com   3
3@3.com   7

What I want to be able to do, is rank the data in this table, based on their referral count, whereby the highest referral count is rank 1 and so on. 我想要做的是根据引荐数对这张表中的数据进行排名,从而使最高引荐数为1级,依此类推。 Then I need to be able to get the rank of that user, based on their email address. 然后,我需要能够根据用户的电子邮件地址获得该用户的排名。

I have only used basic SQL and so I am not so sure how to do this? 我只使用了基本的SQL,所以我不确定该怎么做?

Any help much appreciated! 任何帮助,不胜感激!

This depends a bit on what you mean by "rank". 这有点取决于您所说的“等级”。 The following gets the "dense rank", so ties all have the same value: 以下内容获得“密集等级”,因此领带都具有相同的值:

select 1 + count(distinct u.referralcount)
from users u
where u.referralcount > (select u2.referralcount from users u2 where u2.user = @email);

Edit: Apologies, Gordon Linoff already answered the question more directly. 编辑:抱歉,戈登·利诺夫(Gordon Linoff)已经直接回答了这个问题。 This is what I get for being half distracted while answering. 这就是我在回答时分心的一半。 :) :)


You could use a subquery to grab the ReferralCount for the Email in question, and then get a count of how many user rows have a higher Referral Count. 您可以使用子查询来获取有关电子邮件的ReferralCount,然后获取有多少用户行具有较高的Referral Count的计数。

Your subquery would get the ReferralCount of the user (eg "5" for 1@1.com): select ReferralCount from users where Email=1@1.com . 您的子查询将获取用户的ReferralCount(例如1@1.com为“ 5”): select ReferralCount from users where Email=1@1.com

Then if you use that as a subquery in a where clause, you could count how many users have a higher ReferralCount (remember to add 1 to the rank. If it returns "1" higher user, then you should output "2" as the current user's rank): 然后,如果您将其用作where子句中的子查询,则可以计算出具有更高ReferralCount的用户数(请记住将排名增加1。如果返回“ 1”的较高用户,则应将“ 2”输出为当前用户的排名):

select 
    (count(*) + 1) as Rank
from
    users
where
    ReferralCount > (select ReferralCount from users where Email=1@1.com)

This will output "2", because only 3@3.com has more referrals than 1@1.com. 这将输出“ 2”,因为只有3@3.com具有比1@1.com更多的引荐。

You may want to skip users with the same ReferralCount. 您可能要跳过具有相同ReferralCount的用户。 Eg if three users all have "10" referrals, and the user you're querying has "9", then the above query would output a Rank of "4", even though 9 referrals is only in second place behind 10. If you would rather it return a rank of "2" (second place), then you could get a distinct count of the ReferralCount: 例如,如果三个用户都具有“ 10”个引荐,而您要查询的用户具有“ 9”,则即使9个引荐仅排在10位之后,第二个查询也会输出排名“ 4”。宁愿返回等级“ 2”(第二位),那么您也可以得到ReferralCount的独特计数:

select 
    (count(distinct ReferralCount) + 1) as Rank
from
    users
where
    ReferralCount > (select ReferralCount from users where Email=1@1.com)

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

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