Results 1 to 5 of 5

Thread: MySQL/php help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2008
    Posts
    268

    MySQL/php help

    Hey guys,

    Quick question about mysql, let's say I want to pull data from a table, limit 100 results, where 'score' is highest, from high to low, I'd get results like this, perhaps:

    name | score
    ------------------
    tom | 50
    joe | 45
    bill | 40
    .......

    and so on. Now, what I would like to be able to do is easily find a given name's 'rank', which would be out of 100, sorting 'score' as I did above. 'tom' would be #1, for instance, Joe #2, and Bill #3.

    What I'm wondering is, is there an easy way to make a mysql request to retrieve 'where name = "tom"' and also retrieve the number of rows from the top that the entry sits at, ordered by score highest to lowest.

    Any help is appreciated, thanks a lot!

    David

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: MySQL/php help

    a query like this is what your looking for:

    "SELECT name, score FROM mytable ORDER BY score ASC LIMIT 100"

    Select name and score from table ordering them by score in ascending order from the first 100 rows.
    My usual boring signature: Something

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2008
    Posts
    268

    Re: MySQL/php help

    Right, but I need to return the number which it ranks from that query.

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: MySQL/php help

    use a while loop with a count increment

    PHP Code:
    $number 1;
    while(
    blahblahblah) {
         echo 
    $number;
         
    // blah
         
    $number++;

    My usual boring signature: Something

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: MySQL/php help

    Code:
    set @row = 0;
    select
      @row := @row+1 'Index',
      name 'Name',
      score 'Score'
    order by score desc
    limit 100;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width