Filtering date data in MySQL using WHERE against a date
In a previous post I described a method of using DATE_FORMAT to sort data and search against it. That post described how you can sort your data and then use a WHERE clause against it. It basically described a 2 step process, this is alternative and does it in 1 step. There are benefits to both methods and I leave it up to you to decide which way to go.
N.B. I am sure this method is on a million different websites, I only include it on my blog so that it has both versions.
The SQL:
SELECT * FROM your_table WHERE your_timestamp > '2010-10-09 00:00:00' AND your_timestamp < '2010-10-09 23:59:59'
The Explanation:
It's quite simple really; MySQL performs a comparison where your value in your DATETIME field is greater than the minimum reference and less than the maximum. In the case above it selects all dates from a specific day (2010-10-09), but in theory you could do it on many different ranges.
I hope this helps.
N.B. I am sure this method is on a million different websites, I only include it on my blog so that it has both versions.
The SQL:
SELECT * FROM your_table WHERE your_timestamp > '2010-10-09 00:00:00' AND your_timestamp < '2010-10-09 23:59:59'
The Explanation:
It's quite simple really; MySQL performs a comparison where your value in your DATETIME field is greater than the minimum reference and less than the maximum. In the case above it selects all dates from a specific day (2010-10-09), but in theory you could do it on many different ranges.
I hope this helps.
Labels: database timestamp, databases, DATETIME, MySQL, SQL
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home