MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 4th, 2005, 03:44 AM
mikeruss mikeruss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 3 mikeruss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 7 sec
Reputation Power: 0
index retrieval

this is probably a dumb question but ...

I want to retrieve the index sequence of a field - preferably without retrieving the whole table

so if I have a data item called score which is indexed and contains a numeric value, I want to know it is say the 5th highest in the table.

I can do a ORDER by score DESC, but this is going to retrieve the whole table.

Is there a way to retrieve the sequence directly from the index ?
thanks .. Mike

Reply With Quote
  #2  
Old October 4th, 2005, 07:37 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
SELECT score FROM table ORDER BY score DESC LIMIT 5


Note, you can pick and choose your fields, instead of doing SELECT * you can do SELECT field1, field2, etc...

Reply With Quote
  #3  
Old October 4th, 2005, 07:59 AM
mikeruss mikeruss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 3 mikeruss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 7 sec
Reputation Power: 0
clarification

thanks - but I need to clarify

5th was just an example - I dont know the ranking for each record which means I need to read the whole table in score sequence until I find the sequence for this record ?

as I do this a lot - I am looking for a way to optimise the retrieval.

Mike

Reply With Quote
  #4  
Old October 4th, 2005, 08:02 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 34 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
Or if you ONLY want the fifth element:
mysql Code:
Original - mysql Code
    SELECT * FROM table WHERE 4 = (SELECT COUNT(*) FROM table AS t1 WHERE t1.score > table.score);

Note, this only works on MySQL version 4.1 and newer. In earlier versions, subqueries weren't supported yet.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 264

Reply With Quote
  #5  
Old October 4th, 2005, 08:07 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 34 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
So what you actually want is a way to retrieve the rank of a know item?
try this:
mysql Code:
Original - mysql Code
    SELECT COUNT(*) + 1 AS rank FROM table WHERE score > (SELECT score FROM table where id = 'wantedid');

Where "id = 'wantedid' can be replaced by anything that identifies the record you want the ranking for.

Reply With Quote
  #6  
Old October 4th, 2005, 11:11 AM
mikeruss mikeruss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 3 mikeruss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 7 sec
Reputation Power: 0
Thanks

thanks itsacon - thats very helpful

Reply With Quote
  #7  
Old October 4th, 2005, 11:12 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2Folding Points: 682581 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 34 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
Good. that was my intention

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > index retrieval


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway