Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql - How to get a row number after Order by?

Tags:

php

mysql

Let's say I have a table with the following columns:


p_id

userid

points


Let's say these columns have over 5000 records. So we actually have users with points. Each user has an unique row for their point record. Imagine that every user can get points on the website by clicking somewhere. When they click I update the database with the points they get.

So we have a table with over 5000 records of people who have points, right? Now I would like to order them by their points (descending), so the user with the most point will be at the top of the page if I run a MySQL query.

I could do that by simply running a query like this:

SELECT `p_id` FROM `point_table` ORDER BY `points` DESC

This query would give me all the records in a descending order by points.

Okay, here my problem comes, now (when it is ordered) I would like to display each user which place are they actually. So I'd like to give each user something like this: "You are 623 of 5374 users". The problem is that I cannot specify that "623" number.

I would like to run a query which is order the table by points it should "search" or count the row number, where their records are and than return that value to me.

Can anyone help me how to build a query for this? It would be a really big help. Thank you.

like image 271
user1406071 Avatar asked Sep 09 '12 17:09

user1406071


People also ask

How can I get row number in MySQL?

MySQL ROW_NUMBER() Using Session Variable Execute the below statement that add the row number for each row, which starts from 1: SET @row_number = 0; SELECT Name, Product, Year, Country, (@row_number:=@row_number + 1) AS row_num.

How do I fetch a specific number of rows in SQL?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).

Is there a Rowid in MySQL?

No, MySQL doesn't expose a ROWID type like Oracle does.

What is Rownum equivalent in MySQL?

There is no equivalent of ROW_NUMBER() in MySQL for inserting but you can achieve this with the help of variable.


2 Answers

Row number after order by

SELECT ( @rank:=@rank + 1) AS rank, m.* from                                   
(                            
  SELECT a.p_id, a.userid                                                   
  FROM (SELECT @rank := 0) r, point_table a 
  ORDER BY a.points DESC                                                     
) m
like image 148
Megu bhai Avatar answered Oct 20 '22 07:10

Megu bhai


This answer should work for you:

SET @rank=0;
SELECT @rank:=@rank+1 AS rank, p_id FROM point_table ORDER BY points DESC;

Update: You might also want to consider to calculate the rank when updating the points and saving it to an additional column in the same table. That way you can also select a single user and know his rank. It depends on your use cases what makes more sense and performs better.

Update: The final solution we worked out in the comments looked like this:

SELECT
rank, p_id
FROM
    (SELECT
     @rank:=@rank+1 AS rank, p_id, userid
     FROM
     point_table, (SELECT @rank := 0) r
     ORDER BY points DESC
    ) t
WHERE userid = intval($sessionuserid); 
like image 25
sprain Avatar answered Oct 20 '22 07:10

sprain