SQL question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

SQL question

Post 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.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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 :)
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post 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;
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

I found the solution!!!!!!

Code: Select all

SELECT * FROM packaging WHERE 12 BETWEEN amin AND amax
Post Reply