-1

I have the following table structure for a table Player

Table Player {  
Long playerID;  
Long points;  
Long rank;  
}

Assuming that the playerID and the points have valid values, can I update the rank for all the players based on the number of points in a single query? If two people have the same number of points, they should tie for the rank.

UPDATE:

I'm using hibernate using the query suggested as a native query. Hibernate does not like using variables, especially the ':'. Does anyone know of any workarounds? Either by not using variables or working around hibernate's limitation in this case by using HQL?

user2310422
  • 555
  • 3
  • 9
  • 22
  • this serves your purpose http://stackoverflow.com/questions/11963818/row-number-equivalent-in-mysql-for-inserting – sarwar026 Jul 07 '13 at 06:30

2 Answers2

0
select  @a:=@a+1 position,
      id, 
      sum(item_1+item_2+item_3) as item_sum 
 from yourtable , (SELECT @a:= 0) AS a
 group by id 
 order by item_sum desc;

you can also try this

SET @position=0;
SELECT   @position:= @position+1 AS position,   
          id, 
          sum(item_1+item_2+item_3) as item_sum 
     from yourtable
     group by id 
     order by item_sum desc;

or try this one.......

SELECT @rn:=@rn+1 AS position, item_sum , id
FROM (
  select 
      id, 
      sum(item_1+item_2+item_3) as item_sum 
 from yourtable 
 group by id 
 order by item_sum desc
) t1, (SELECT @rn:=0) t2;
Dinup Kandel
  • 2,457
  • 4
  • 21
  • 38
0

// increment num by one for each result row @num:=@num+1 with aliasname position and define it initially with 0,or by what to want to be..

select @num:=@num+1 position,
  id, 
  sum(item_1+item_2+item_3) as item_sum 
  from yourtable,(SELECT @num:=0) r
group by id 
order by item_sum desc;
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41