如果以前的值相同,则mysql的排名相同

时间:2016-09-09 18:17:10

标签: php mysql

我在MySQL中有以下查询:

SELECT COUNT(c2.cus_referrer_id) as total_referrals,
                @n := @n + 1, @r := IF(@c = COUNT(c2.cus_referrer_id), @r, @n) rank, @c := COUNT(c2.cus_referrer_id)
                FROM customers c1
                INNER JOIN customers c2 ON c1.id = c2.cus_referrer_id,
                (
                    SELECT @r := 0, @n := 0, @c := NULL
                ) i
                WHERE c2.cus_referrer_id!='0'
                AND (DATE(c2.cus_date_added) BETWEEN '2014-09-15' AND '2016-10-30')
                AND c1.is_deleted='0' AND c2.is_deleted='0'
                GROUP BY c2.cus_referrer_id
                ORDER BY total_referrals DESC LIMIT 50

我想要做的是根据总推荐次数显示排名。如果有相同的推荐,那么保持排名相同,下一个推荐将被跳过排名..

例如:

Array
(
[0] => Array
    (
        [total_referrals] => 3
        [@n := @n + 1] => 3
        [rank] => 2
        [@c := COUNT(c2.cus_referrer_id)] => 3
    )

[1] => Array
    (
        [total_referrals] => 1
        [@n := @n + 1] => 1
        [rank] => 0
        [@c := COUNT(c2.cus_referrer_id)] => 1
    )

[2] => Array
    (
        [total_referrals] => 1
        [@n := @n + 1] => 2
        [rank] => 1
        [@c := COUNT(c2.cus_referrer_id)] => 1
    )

[3] => Array
    (
        [total_referrals] => 1
        [@n := @n + 1] => 4
        [rank] => 3
        [@c := COUNT(c2.cus_referrer_id)] => 1
    )

)

在上述情况中,在最后3条记录中,总推荐次数相同:1

所以,第四名将排在第5位。

我需要输出排名如:1,2,2,2。

我在这里做错了什么?

0 个答案:

没有答案