PDA

View Full Version : MySQL simple question on dates




carlosbutler
Mar 31, 2009, 05:15 PM
Is it possible to be able to have an SQL query that is able to search through dates with +/- a few days?

For example if looking for a record with a date on 2009-04-01, could it be expanded to have a range from 2009-03-30 -> 2009-04-03?

I can guess you are able to do something like:

select * from 'something' where date < '2009-04-03' and date > '2009-03-30';

But I am not sure how to get it so that it adds/decreases the dates for the search statement

Thanks



belvdr
Mar 31, 2009, 05:24 PM
Not automatically, but if you don't want to calculate the days:


WHERE <column> BETWEEN DATE_SUB('2009-04-01', INTERVAL 3 DAY) AND DATE_ADD('2009-04-01', INTERVAL 3 DAY)

carlosbutler
Mar 31, 2009, 07:35 PM
ok thanks, worked like a charm :D