0

I've a table called "Ranking" which basically saves users ranking position sorted by their "points". It's:

*Id - Integer - A.I. (It's the position. 1 = first one. Highest points)
*IdUser - Integer
*PointsNow - Integer
*PointsBefore - Integer
*PositionBefore - Integer

What I'm trying to achieve is to have some method to know if an user went into a highest rank position or if he's lower now.

So for example, those are some entries:

1 | 2236 | 41 | 0 | 0

2 | 551 | 40 | 0 | 0

3 | 1557 | 34 | 0 | 0

So I though that I could achieve this doing this:

*Delete all entries

*Reset the autoincrement to 1

*Copy PointsNow to PointsBefore and Id to PositionBefore.

I'm not sure if I should do it this way, or if there's a more logical option. I'd appreciate some help.

Thanks

Community
  • 1
  • 1
Reinherd
  • 5,476
  • 7
  • 51
  • 88

1 Answers1

1

Try this:

select @rownum:=@rownum+1 `rank`, u.idUser 
from User u, (SELECT @rownum:=0) r 
order by PointsNow desc;

If you just want to order the user based on the ranking:

SELECT * FROM User
ORDER BY PointsNow DESC;

Sample data:

CREATE TABLE User(
IdUser Integer primary key,
PointsNow Integer,
PointsBefore Integer,
PositionBefore  Integer);

INSERT INTO User(IdUser,PointsNow,PointsBefore,PositionBefore)
VALUES (2236,41,0,0),(551,40,0,0),(1557,34,0,0);

SQL FIDDLE DEMO

Important to note is that I have omitted the id column. Use triggers to automate the query. See for more info on the ranking calculation. MySQL update statement to store ranking positions.

Community
  • 1
  • 1
Mr. Radical
  • 1,847
  • 1
  • 19
  • 29