Page 1 of 1

SQL question

Posted: Thu Aug 21, 2003 5:04 pm
by matthiasone
I am try to query 1 row of data based on a range.

when I ran this query:

Code: Select all

SELECT * FROM packaging WHERE amin >= 4 AND amax <= 4
I get no rows returned.

if I ran this query:

Code: Select all

SELECT * FROM packaging WHERE amin >= 4 OR amax <= 4
I get all the rows.

Is there a better way to write the query.

Posted: Thu Aug 21, 2003 6:06 pm
by Coco

Code: Select all

SELECT * FROM packaging WHERE amin >= 4 AND amax <= 4
the only time this can return anything is when amin==4

eg amin>4 AND amin<4 will always be false
yet amin>=4 AND amin <=4 returns true if amin==4
so you might as well be saying WHERE amin = 4

amin>= OR amin <= 4 will always be true
if you have difficulty, turn the signs into words...

amin is greater than or equal to 4
is the same as
amin is greater than 3

amin is less than or equal to 4
is the same as
amin is less than 5

amin is greater than 3 OR less than 5 is always true, since its always either less than 5 or greater than 3
replace the OR and the only thing that fits is 4
you get the idea... :)
such logic can be hard to master, but once youve got it straight in your head, it all becomes alot easier :)

Posted: Thu Aug 21, 2003 6:13 pm
by matthiasone
I already tried that logic....

Code: Select all

SELECT * FROM packaging WHERE amin > 3 AND amax < 5
Got no rows returned.

here is the code running it.

Code: Select all

$query = "SELECT * FROM packaging WHERE amin > ".($audio-1)." AND amax < ".($audio+1);
  echo $query.'<br>';
  $result = query_db($query, '2');
  $num_rows = mysql_num_rows($result);
  echo $num_rows.'<br>';
  for ($i = 0; $i <= $num_rows -1; $i++)
  &#123;
    $info = mysql_fetch_assoc($result);
    echo $info&#1111;'package'].'<br>';
  &#125;

Posted: Thu Aug 21, 2003 6:27 pm
by matthiasone
I found the solution!!!!!!

Code: Select all

SELECT * FROM packaging WHERE 12 BETWEEN amin AND amax