Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting record with closest key value 1

Status
Not open for further replies.

RobBroekhuis

Technical User
Oct 15, 2001
1,971
US
I'm looking for the syntax that will find the record whose key is equal to, or as close as possible but smaller than, the value sought. Can it be done in a simple query, or do I have to use a MAX of all that are <= to get this job done?
Thanks in advance,


Rob
[flowerface]
 
OK, thanks. So it's two steps - first find the key value, then select again to get the field values associated with that key?
select * for key=closestkey

Rob
[flowerface]
 
Something like this might meet your requirements:
Code:
SELECT [i]fields[/i]
  FROM [i]daTable [/i]
  WHERE [i]key[/i] <= [i]value [/i]
  ORDER BY [i]key[/i] DESC
  LIMIT 1
This sorts the records that have a key less than or equal to value into descending order but then limits the result set to the first record only which is presumably the one you want. Obviously you replace the italicised fields with your own names.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top