tags:

views:

22

answers:

4

Im pulling information from a database and ive got the query working fine except i'd like to only select conent from a certain date range. Each row has a field with the created date stored as a DATETIME field. What is the basic syntax?

+2  A: 
SELECT fields
  FROM table
 WHERE date BETWEEN '$startDate' AND '$endDate'

Dates in MySQL are in YYYY-MM-DD format, so the actual query would look like:

SELECT fields
  FROM table
 WHERE date BETWEEN '2010-10-01' AND '2010-09-25'
NullUserException
Is this formatting style "OMG Ponies" invention or some common convention? ;-)
zerkms
@zerkms I like how it makes it easier to visualize what you are doing. Yes, I believe I started doing it after seeing one of OMG Ponies' posts.
NullUserException
the field is a DATETIME field which i believe stores the date as a timestamp in the format YYYY-MM-DD HH-MM-SS but i think the basic syntax will work regardless
Baadier
+1  A: 
WHERE `date_field` BETWEEN '2010-09-21 12:13:14' AND '2010-09-28 12:13:14'
zerkms
+1  A: 

Here's the link

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between

select * from table where datetime between DATE1 and DATE2
Galen
+1  A: 
SELECT * FROM table
WHERE DateTime
BETWEEN time1 AND time2
JoshD