2

This is my table

   id   |   name   |  userid  |  score  |      date      |
   ------------------------------------------------------------
    1   |   john   |    1     |   44    |  2013-03-2 
    2   |   mary   |    2     |   59    |  2013-03-5  
    3   |   john   |    12    |   38    |  2013-03-8 
    4   |   elvis  |    3     |   19    |  2013-02-10 
    5   |   john   |    11    |   1002  |  2013-01-11
    6   |   johnah |    10    |   200   |  2013-01-11

I want to show number of position of my given userid order by highest score.

if i give userid 12 in my query then it show position number 5 for user 12 depend on highest score

Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • I see it position 5 based on the data you provided – CodeBird Apr 01 '14 at 08:21
  • Select all records from the database and use `ORDER BY score`. Then loop through the results and search for the given user ID. Count how many times the loop was entered before the user ID was found and you got your result. – t.h3ads Apr 01 '14 at 08:22
  • http://stackoverflow.com/questions/12541040/how-to-calculate-rank-from-mysql-table – Hanky Panky Apr 01 '14 at 08:22

2 Answers2

1

I think this is what you want.

SELECT COUNT(*)+1 FROM table_name WHERE userid!=12 AND 
score>(SELECT score FROM table_name WHERE userid=12);
CodeBird
  • 3,883
  • 2
  • 20
  • 35
0

Try This :

SELECT *  
FROM table_name 
WHERE id =(SELECT max( userid ) FROM table_name)
Taryn
  • 242,637
  • 56
  • 362
  • 405
Benjamin
  • 2,257
  • 1
  • 15
  • 24