I have a MySql database that uses a timestamp field to track when the entry was inserted.
Now I want to query the table for entries by a date range.
I would prefer to use MySql to extract the records instead of extracting all records then iterating and comparing dates in php.
So I have a start-date and an end-date. I have tried YYYYMMDD and YYYY-MM-DD format with no success on these two kinds of queries:
// this doesn't appear to be working
SELECT COUNT(*) FROM table_name
WHERE DATE(timestamp_row) BETWEEN '{$date1}' AND '{$date2};
// this doesn't appear to be working either
SELECT COUNT(*) FROM table_name
WHERE DATE(timestamp_row) >= {$date1}
AND DATE(timestamp_row) <= {$date2};
It appears that the Mysql DATE() function turns the timestamp into this format: YYYY-MM-DD
Is there a way to do this.
timestamp_row
not a TIMESTAMP/DATETIME/DATE type column? – Dan Grossman{$date1}
, etc? – sdleihssirhcDATE()
in all your queries will prevent any use of indexes on the timestamp column. Your whole table will have to be scanned to resolve the query. If you can, compare the column directly to a date with time. – Dan Grossman