Development tips, help, and suggestions for building performant websites
Uncategorized
MySQL – Query for Date Ranges Using Interval
09 months ago
by Mike Purcell
in Uncategorized
If you ever need to query for a date range on a date column, try the following example.
/* All records that are within a week */ SELECT * FROM <table_name> WHERE <some_date_column> BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW(); /* All records within the last 2 hours */ SELECT * FROM <table_name> WHERE <some_date_column> BETWEEN DATE_SUB(NOW(), INTERVAL 2 HOUR) AND NOW();
