How do I query mysql RANK?

Asked 2 years ago, Updated 2 years ago, 41 views

Hello, I'm trying to print out the ranking on mysql.

SELECT * FROM (
                SELECT
                    cu_distribution,
                    count(cu_status),
                    @curRank := @curRank + 1 AS rank
                FROM 
                cs_customer p, (SELECT @curRank := 0) r where cu_status=1 
                group by cu_distribution order by count(cu_status)) AA

View results

That's what happens. Why is that?

mysql php

2022-09-21 18:18

1 Answers

I worked on it for hours today, and it finally worked out. If you're stuck like me, please do it like below.

SELECT cu_distribution, status, @Rank:=@Rank + 1 ASuser_rank
FROM
(
    SELECT cu_distribution, count(cu_status) AS status
    FROM cs_customer
    where cu_status=1
    GROUP BY cu_distribution
    ORDER BY status DESC
) Sub1
CROSS JOIN (SELECT @Rank:=0) Sub2

You can do it like this.


2022-09-21 18:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.