Question:
Query mysql db for a number that lies in a range in a recordset?
anonymous
2007-08-07 20:03:47 UTC
Hi. I have the following columns of data in a MySQL database: lowrange, highrange, datainfo.

If I have the following datasets:

lowrange highrange datainfo
300 399 random info here
400 499 more random info
500 599 random here

(Note...each row and number is unique. No number appears in more than one recordset.)

If I query the database for the number 458, I want to return:
400 499 more random info.

So, how do I enter that query? Thank you.
Three answers:
anonymous
2007-08-07 20:29:49 UTC
SELECT *

FROM table

WHERE lowrange <= 458

AND highrange >= 458



Just to note, given your dataset, you can also do this with a single column, lowrange, plus a LIMIT and ORDER BY clause:



SELECT *

FROM table

WHERE lowrange <= 458

ORDER BY lowrange DESC

LIMIT 1
Krystine
2007-08-07 20:16:44 UTC

$record=458;

$sql="SELECT `lowrange`, `highrange`, `datainfo` FROM `table` WHERE `lowrange`<".$record." AND `highrange`>".$record."";



$result = mysql_query($sql);

$row = mysql_fetch_row($result);

echo $row['0']." ".$row['1']." ".$row['2'];

?>





An example of the query you need to run.



SELECT `lowrange`, `highrange`, `datainfo` FROM `table` WHERE `lowrange`<458 AND `highrange`>458;
marsulein
2007-08-07 20:11:02 UTC
SELECT * FROM TABLE

WHERE lowrange >= 458 OR highrange <= 458


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...