SQL Select with < and >

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
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

SQL Select with < and >

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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";
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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";
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post by ibanez270dx »

Thanks guys! Is there a listing of SQL operators somewhere?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

always look to the source
Post Reply