0

I have a table user

Name     | Poin 
================== 
user1    | 20 
user2    | 30 
user3    | 80 
user4    | 60 
user5    | 10 
user6    | 85 

And I have SQL query

SELECT * 
FROM user 
ORDER BY poin

It would appear that the data sequence based on points.

But what I need is data like this (for example, I was user1):

Position 1 : user6 - 85 point 
Position 2 : user3 - 80 point 
Position 3 : user4 - 60 point 

You are position 5 : user1 - 20 point


UPDATE

I use this sql

SELECT x.name, x.position
FROM (SELECT t.user,
            @rownum := @rownum + 1 AS position
            FROM user t
           JOIN (SELECT @rownum := 0) r
           ORDER BY t.poin DESC) x
WHERE x.user = 'user1'

6 Answers 6

1

This will give current rank for user1:

SELECT count(*) AS rank
FROM user
WHERE poin >= (SELECT poin FROM user WHERE name = 'user1')

Small issue with this query is that if another user has the same points, it will be assigned the same rank - whether it is correct, it is questionable.

If you want to simply add rank for every user, use this:

SELECT
    @rank:=@rank+1 AS rank,
    name,
    poin
FROM user,
    (SELECT @rank:=0) r
ORDER BY poin DESC

You can use small variation of this query to get rank of single user, but avoid issue of the same ranking ambiguity:

SELECT *
FROM (
    SELECT
        @rank:=@rank+1 AS rank,
        name,
        poin
    FROM user,
        (SELECT @rank:=0) r
    ORDER BY poin DESC
) x
WHERE name = 'user1'
Sign up to request clarification or add additional context in comments.

3 Comments

thx for response @mvp.. I use the first choice of you. but there was a slight problem when there is a point value equal to User1. how do i fix this .. thx before.. :)
I have updated my answer to give an option to disable "ties" - all users will have unique rankings, even if they have the same score
GREAT.. this work. why did not I think to do multiple queries like that :p .... ok thank you so much @mvp .. Your partner helps .. ;)
0

select * from user order by poin desc

Hope this helps

1 Comment

@johannes: is that so? Your code showed you knew about queries and all. So dint made an explanation
0
SELECT * FROM user ORDER BY poin DESC

The ORDER BY keyword is used to sort the result-set by a specified column.

The ORDER BY keyword sorts the records in ascending order by default.

If you want to sort the records in a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

3 Comments

fauzi wants the position and not description of 'order by'. I really dont understand how this answer helps user.
I understand the question is the he wants to order the users, I think he used position just for example
If I understand correctly, OP needs user current position (rank). Your query simply does not have it
0
SELECT  Name, 
        Poin, 
        @rowNum := @rowNum + 1 AS position
FROM    user
JOIN    (SELECT @rowNum := 0) r
ORDER BY poin;

1 Comment

thank you for the solution, but I can not use it because it has to process all data to rank
0

The following code:

SELECT count(*) AS rank 
FROM user 
WHERE poin >= (SELECT poin FROM user  WHERE name = 'user1')

Has a problem when is has double point in different rows.

Comments

-1
SELECT  Name, 
        Poin, 
        @rowNum := @rowNum + 1 AS position
FROM    user
JOIN    (SELECT @rowNum := 0) r
ORDER BY poin DESC;

2 Comments

thank you for the solution, but I can not use it because it has to process all data to rank
@a.fauzi: Glad to hear that. If this is the answer that helped you and brought you wonders then just click on the "right/tick" in the upper left side down to "-1" scroll-bar so that other users will find it in short span of time if they'd look for exactly or relevant answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.