Page 1 of 1

SQL Select with < and >

Posted: Tue Oct 03, 2006 3:38 pm
by ibanez270dx
Hi,
I have a PHP search feature in which the user specifies a date span which results in selecting all the data entries between the two dates. For this, I am using timestamps. My SQL query looks like this:

Code: Select all

$sql = "SELECT * FROM downtime WHERE dwntime_ts1 > '$ts1' < dwntime_ts2 ORDER BY dwntime_ts1";
where dwntime_ts1 is the "start date" of the entry in timestamp form, dwntime_ts2 is the "end date" of the entry in timestamp form, and $ts1 is the user-defined timestamp required in the search. However, no matter what dates I enter, it just keeps selecting the entire table's information instead of the specific rows. I have a feeling that I am going about this the wrong way... not too sure, so any help is appreciated!

Thanks!
- Jeff

Posted: Tue Oct 03, 2006 3:41 pm
by Burrito
you need an AND in there.

Code: Select all

$sql = "SELECT * FROM downtime WHERE dwntime_ts1 > '$ts1' AND dwntime_ts2 < '$ts1' ORDER BY dwntime_ts1";

Posted: Tue Oct 03, 2006 3:45 pm
by blackbeard
You can also use the BETWEEN operator

Code: Select all

$sql = "SELECT * FROM downtime WHERE  '$ts1 BETWEEN dwntime_ts1 AND dwntime_ts2 ORDER BY dwntime_ts1";

Posted: Tue Oct 03, 2006 3:46 pm
by ibanez270dx
Thanks guys! Is there a listing of SQL operators somewhere?

Posted: Tue Oct 03, 2006 3:47 pm
by Burrito
always look to the source