1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved [MySQL] Get ranking

Discussion in 'Development' started by Muqsit, May 22, 2017.

  1. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    How do I get ranking of a specific player from a MySQL table.
    Here's the table's schema:
    Code:
    +--------+---------+
    | Player | Balance |
    +--------+---------+
    | Edeire | 120044  |
    | Tajfhg | 385848  |
    | Hafjw  | 38288   |
    +--------+---------+
    
    I know this would arrange the data in ascending order by "Balance"
    PHP:
    /** @var \mysqli $db */
    $db->query("SELECT Player, Balance FROM table ORDER BY Balance");
    What I want is the ranking of "Edeire" based on all the balances.
    An alternative to a while loop like the one below.
    PHP:
    /** @var \mysqli $db */
    $query $db->query("SELECT Player, Balance FROM table ORDER BY Balance");
    $rank 0;
    while(
    $res $query->fetch_array()){
        ++
    $rank;
        if(
    $res["Name"] == "Edeire"){
            break;
        }
    }
    if(
    $rank !== 0){
        echo 
    "Edeire is ranked $rank based on their economic status.";
    }
     
    Last edited: May 22, 2017
  2. Keith

    Keith Spider Jockey

    Messages:
    42
    GitHub:
    k3ithos
    PHP:
    /** @var \mysqli $db */
    $query $db->query("SELECT Player, Balance FROM table ORDER BY Balance DESC");
    $rank 0;
    $player = array();
    while(
    $res $query->fetch_array()){
        ++
    $rank;
        
    $player[$res['Name']] = $rank;
    }
    if(
    $rank !== 0){
        echo 
    "Edeire is ranked ".$player['Edeire']." based on their economic status.";
    }
     
  3. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    PHP:
    SELECT COUNT(Player)+AS rank 
    FROM table 
    WHERE Balance 
    > (SELECT Balance FROM table WHERE Player='Edeire');
    Code:
    +------+
    | rank |
    +------+
    |    2 |
    +------+
    1 row in set (0.00 sec)
    
     
    Legoboy0215, Muqsit and jojoe77777 like this.
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I'm not sure about the exact performance, but a JOIN query might be better than doing a new SELECT every time: http://sqlfiddle.com/#!9/f48b5/5 (if it doesn't work, see the spoiler below)
    Note that I used < instead of <= in the ON clause, and had a COUNT() +1 when defining the rank column. This is because if I use <=, duplicates will also be counted and we will have "1 2 4 4 5 6" rather than "1 2 3 3 5 6" in the result.
    Also note that HAVING rather than WHERE is used.

    Code:
    CREATE TABLE tbl (
      name VARCHAR(20) PRIMARY KEY,
      bal INT
    );
    INSERT INTO tbl (name, bal) VALUES
      ('alice', 5),
      ('bob', 3),
      ('carol', 6),
      ('dan', 9),
      ('erin', 4),
      ('frank', 5);
    
    
    Code:
    SELECT * FROM tbl ORDER BY bal DESC;
    SELECT t1.name, COUNT(t2.name) + 1 AS rank FROM tbl t1
      LEFT JOIN tbl t2 ON t1.bal < t2.bal
      GROUP BY t1.name
      ORDER BY rank;
    SELECT t1.name, COUNT(t2.name) + 1 AS rank FROM tbl t1
      LEFT JOIN tbl t2 ON t1.bal < t2.bal
      GROUP BY t1.name
      HAVING t1.name = 'alice'
    
    upload_2017-5-23_15-9-36.png
     

    Attached Files:

    Last edited: May 23, 2017
    Muqsit likes this.
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Thank you, I had no idea joining queries and HAVING was even a thing *-*
     
    Legoboy0215 likes this.
  6. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    @Muqsit don't listen to @SOFe, join is slow!
    Tested with 1006 rows.
    [​IMG]
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    OMG that's hella slow, I'd rather stick with my while loop!
     
  8. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    Did you even test how long it takes with the while loop? @Muqsit?
    with #2: 0.014381885528564
    with #3: 0.0037500858306885
    with #4: 0.6971070766449
     
    SOFe likes this.
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It was an exaggerated joke :p
    Btw thanks @SOFe. I guess"HAVING" isn't commonly used or something, I didn't know it existed.
     
    Last edited: May 24, 2017
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Any time complexity data?
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.