Mysql Query With Rank


Subject: Mysql Query With Rank
Hey guys...

Yeah its me again :shock: :mrviolet: 8)

Another thing i need to figure out...

the DB structure is with id, user_id, score,

i have a user_id and a score (number)... and i want to count the entries from the DB... order by score and gives back the Rank/place/count if the user_id matches...

for example:

id=1 user_id=2 score=50 <--------- 1. place
id=2 user_id=3 score=23 <--------- 3. place
id=3 user_id=7 score=34 <--------- 2. place

i hope you can understand what i mean, and help me with that... thanks...

Subject: Re: Mysql Query With Rank
Code: [Download] [Hide]
  1. SELECT user_id, username, user_rank, user_rank2, user_rank3 ... 
  2. FROM USERS_TABLE 
  3. ORDER BY score DESC 
The PHP code should'nt be too difficult ;-). Something like
Code: [Download] [Hide]
  1. <?php $result = $db->sql_result($sql);
  2. $place = 0;
  3. while ($row = $db->sql_fetchrow($result))
  4. {
  5. echo 'Hi I\' ' . $row['username'] . ', id ' . $row['user_id'] . '. My place is ' . ++$place . ', and my first rank is ' . $row['user_rank'] . '<br />'; //feel free to display more ranks
  6. }

Profile PM  
Subject: Re: Mysql Query With Rank
Thanks Informpro,

you point me in the right direction ;)

I´ve done it like this... maybe you can take a look.. the code works well, but maybe its not perfect.. let me know if i can tweak it a bit 8)

Spoiler: [ Show ]


Its for a User Dynamic Signature with data based from Users Profile & Course Handicap site for our Golf MMoG...

herzscheisse

Last edited by KugeLSichA on Sun 03 Apr, 2011 16:22; edited 1 time in total
Subject: Re: Mysql Query With Rank
(use code syntax=php !)
For the group query, use something like
Code: [Download] [Hide]
  1. AND g.group_id NOT IN (1, 2, 3, 4, 5, 43) 
Much more readable than
Code: [Download] [Hide]
  1. AND g.group_id <> 1 
  2. AND g.group_id <> 2 
  3. AND g.group_id <> 3 
  4. AND g.group_id <> 4 
  5. AND g.group_id <> 5 
  6. AND g.group_id <> 43 
For everything else, it looks good. Nice job ;-).

Profile PM  
Subject: Re: Mysql Query With Rank
Ok thanks :)


Page 1 of 1


  
You cannot post new topics
You cannot reply to topics
You cannot edit your posts
You cannot delete your posts
You cannot vote in polls
You cannot attach files
You can download files
You cannot post calendar events

   

This is a "Lo-Fi" version of our main content. To view the full version with more information, formatting and images, please click here.

Powered by Icy Phoenix based on phpBB
Generation Time: 0.2129s (PHP: 15% SQL: 85%)
SQL queries: 11 - Debug Off - GZIP Enabled