Find position in a MySQL database

Discussion in 'PHP' started by okgaz, Feb 25, 2008.

  1. #1
    Ok this is a pretty dumb question..

    I want to find the position of an entry in a database filled with scores so that I can say.

    "This player is ranked 13 out of 108"

    I can use "COUNT *" to retrieve the 108 but what is the sql command to retrieve the 13?

    Thanks!
     
    okgaz, Feb 25, 2008 IP
  2. able

    able Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like:

    select * from players order by rank desc limit 13, 1

    Would bring back the player ranked 13th assuming there is a rank column
     
    able, Feb 25, 2008 IP
  3. okgaz

    okgaz Well-Known Member

    Messages:
    450
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Thanks for your reply.

    I don't have a rank column. Here's a simple example of my table:

    player __ score
    _ 1 _____ 10
    _ 2 _____ 4
    _ 3 _____ 3
    _ 4 _____ 15

    + what I want to do is find what position a specific player is in.

    ie. player 2 is 3rd.

    Any ideas how I can do that?
     
    okgaz, Feb 25, 2008 IP
  4. able

    able Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    select * from players order by score desc limit 13, 1
     
    able, Feb 26, 2008 IP
  5. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #5
    If you know player's score (you can select it before anyway) - use a query like that
    SELECT count(*) FROM players WHERE score >=4
    Code (markup):
    where 4 is score of 2nd player
    It will give you quantity of players having score more that your player - and so it will be the place :)
     
    AsHinE, Feb 27, 2008 IP
    okgaz likes this.
  6. okgaz

    okgaz Well-Known Member

    Messages:
    450
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Ah yes that's the one , thanks!
     
    okgaz, Feb 27, 2008 IP
  7. able

    able Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    lol sorry I kinda messed up and ignored what you were actually asking
     
    able, Feb 27, 2008 IP
  8. jamesmoey

    jamesmoey Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    What about this?

    SELECT count(*)+1 AS rank
    FROM _TABLE_ as t1, _TABLE_ as t2
    WHERE t1.player = "Player Name" AND t1.score > t2.score
     
    jamesmoey, Feb 27, 2008 IP