Web Design Forum: Mysql - order and find position (or rank) - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Mysql - order and find position (or rank) Rate Topic: -----

#1 User is online   mrchristoph 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,359
  • Joined: 18-August 09
  • Reputation: 93

Posted 02 February 2012 - 01:47 AM

Hi,

I've got a table of user's times on a game. I have the following query to get the top 10 quickest times by users:

SELECT uid , MIN(time) as mintime FROM scores WHERE score = 100 AND tid = $tid GROUP BY uid ORDER BY mintime ASC LIMIT 10


This works fine, but what I also want to do is display the logged in user's rank or position within this table even if they aren't in the top 10, and I can't think of any efficient way to do this without looping through the entire table with PHP (which is a lot more than 10!). Any ideas?
0

#2 User is offline   hodephdesign 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 129
  • Joined: 18-January 12
  • Reputation: 10
  • Gender:Male
  • Location:Cardiff, Wales
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 02 February 2012 - 10:18 AM

View Postmrchristoph, on 02 February 2012 - 01:47 AM, said:

Hi,

I've got a table of user's times on a game. I have the following query to get the top 10 quickest times by users:

SELECT uid , MIN(time) as mintime FROM scores WHERE score = 100 AND tid = $tid GROUP BY uid ORDER BY mintime ASC LIMIT 10


This works fine, but what I also want to do is display the logged in user's rank or position within this table even if they aren't in the top 10, and I can't think of any efficient way to do this without looping through the entire table with PHP (which is a lot more than 10!). Any ideas?


Been a while since I did something like this, but did it at my last job. I would have used something like "SELECT COUNT(uid) WHERE mintime > $this_users_time".
0

#3 User is online   Renaissance-Design 

  • Available for custom WordPress work
  • View blog
  • Group: Moderators
  • Posts: 3,592
  • Joined: 12-August 10
  • Reputation: 559
  • Gender:Male
  • Location:South Wales
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 02 February 2012 - 10:28 AM

Use

OR uid = $logedin_user

0

#4 User is online   mrchristoph 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,359
  • Joined: 18-August 09
  • Reputation: 93

Posted 03 February 2012 - 04:07 AM

Quote

Been a while since I did something like this, but did it at my last job. I would have used something like "SELECT COUNT(uid) WHERE mintime > $this_users_time".


So I would have to query once to get the user's best time, and then a second query to find all the other records lower than this? Would this work with the grouping by uid, and ordering by the grouped mintime?

Quote

OR uid = $logedin_user


Can you elaborate on this? I don't see how this would work.

Thanks!
0

#5 User is offline   hodephdesign 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 129
  • Joined: 18-January 12
  • Reputation: 10
  • Gender:Male
  • Location:Cardiff, Wales
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 03 February 2012 - 09:12 AM

View Postmrchristoph, on 03 February 2012 - 04:07 AM, said:

So I would have to query once to get the user's best time, and then a second query to find all the other records lower than this? Would this work with the grouping by uid, and ordering by the grouped mintime?


All records higher, if you want a ranking. I'm not sure what you mean about the grouping!
0

#6 User is online   Renaissance-Design 

  • Available for custom WordPress work
  • View blog
  • Group: Moderators
  • Posts: 3,592
  • Joined: 12-August 10
  • Reputation: 559
  • Gender:Male
  • Location:South Wales
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 03 February 2012 - 10:33 AM

View Postmrchristoph, on 03 February 2012 - 04:07 AM, said:

Can you elaborate on this? I don't see how this would work.


In the WHERE clause - the PHP variable is made up, but I assume you have the uid of the logged in user?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users