Jump to content


Pulling The Ten Highest Integers From A Mysql Field

  • Please log in to reply
3 replies to this topic

#1 sdat1333

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 28 June 2006 - 10:49 PM

OK, there's a mysql db

One field will be occupied with integers. I want the system to sort the ten greatest fields in an array or something. Then I need a way to pull the 'id num' (an incremented field) associated with each of the 10 highest integers from that column.

Ideas? Sorry if this is a really easy task, I'm new to PHP/MySQL

Also, if theres an easy way to do this using ADOdb that would be preferred (I would like to keep server CPU load down as much as possible, and ADOdb caches previous queries to reduce load.

Detailed instructions would be helpful, I'm a HUGE PHP/MySQL noob. :) So please don't assume too much knowledge/

#2 jworisek

  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 June 2006 - 03:39 PM

is there going to be duplicate values in the column?

you can do something like this to get the top ten values:

SELECT var FROM table_name ORDER BY var ASC LIMIT 10

and then you can use another query to grab the ID

SELECT var_id FROM table_name where var='$var'

You can do this in one query with subselects but I think its easier to understand if you are new by using multiple queries.

#3 Wildbug

  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 30 June 2006 - 06:24 PM

Wouldn't this be better?

SELECT var_id,var FROM table_name ORDER BY var DESC LIMIT 10

Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#4 jworisek

  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 June 2006 - 06:26 PM

I was working under the assumption that it was 10 highest unique integers and all the IDs for those integers.

If its 10 integers and you will get more than 10 IDs, go with my code, otherwise go with wildbugs

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users