tags:

views:

291

answers:

3

Hi Guys,

I have a mysql table like below:

id     name     points
1      john     4635
3      tom      7364
4      bob      234
6      harry    9857

I basically want to get an individual user rank without selecting all of the users. I only want to select a single user by id and get the users rank which is determined by the number of points they have.

For example, get back tom with the rank 2 selecting by the id 3.

Cheers

Eef

+5  A: 
SELECT  uo.*, 
        (
        SELECT  COUNT(*)
        FROM    users ui
        WHERE   (ui.points, ui.id) >= (uo.points, uo.id)
        ) AS rank
FROM    users uo
WHERE   id = @id
Quassnoi
Cheers mate, works perfectly
Eef
A: 

select id, name, row_number from users order by points desc

Jim P
A: 

im a db noob. can someone speak to efficiency concerns?

compare not storing rank, to storing rank and recalculating whenever it changes, to whatever other solns are possible...

Dustin Getz