0

I have a table team (name,id,points).

i want to find the rank of team based on points.

SELECT *FROM team ORDER BY points DESC

above query sorts result in descending order of points of team. now i want to find the rank of particular team. means the row number of this result set.

HungryDB
  • 555
  • 3
  • 11
  • 30
  • It isnt a duplicate of the question you linked to. The other question wants the rank of the row when only selecting one row. Similar, but different. – Jeff Jan 07 '14 at 14:37

3 Answers3

1

easy way: Count all the teams with more (or equal) points than your desired_team

SELECT Count(*) 
FROM   team 
WHERE  points >= (SELECT points 
                  FROM   team 
                  WHERE  name = "team_name"); 

(you need to include your team so the "list" start with 1.) in that case if your team has the same points than another team this row will say your team is under. if you whant only to list the teams with more points use this:

SELECT Count(*) + 1 
FROM   team 
WHERE  points > (SELECT points 
                 FROM   team 
                 WHERE  name = "team_name"); 

pd: sorry for my english

Hawk
  • 5,060
  • 12
  • 49
  • 74
Melon
  • 874
  • 8
  • 17
-1

Try this. It should give you exactly what you need.

SELECT 
    @i:=@i+1 AS rownumber, 
    t.*
FROM 
    team AS t,
    (SELECT @i:=0) AS test
ORDER BY points DESC
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
-1

Try this:

SELECT IF(@prev <> points, @s:=@s+1, @s:=@s) AS `row_number`, 
  id, name, @prev:=points points
FROM
  team, (SELECT @s:= 1, @prev:= 0) s
ORDER BY points DESC
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79