Newbie question

Newbie question

am 12.05.2006 20:14:51 von Chris Styles

Hi,

I'm learning MySQL, and I am struggling with a few odds and ends. The one
that I can't see how to crack is to search by age.

The records in my database have a datetime field for when they were
inserted.

In my web front end I want to do a "show me entries inserted during the last
[1-9] Days or [1-3] months"

I am guessing the query will be something like

Select * where Date > NOW () -

My PHP script can take account for if it is filtering in days or months, but
I cant work out the actual syntax for subtracting a set number of days or
months from NOW()

Any help gratefully accepted!

Thanks

Chris

Re: Newbie question

am 12.05.2006 22:18:56 von Bill Karwin

Chris Styles wrote:
> Select * where Date > NOW () -

SELECT * FROM `tablename` WHERE `datecol` > NOW() - INTERVAL 9 DAY

or

SELECT * FROM `tablename` WHERE `datecol` > DATE_SUB(NOW(), INTERVAL 9 DAY)

See: http://dev.mysql.com/doc/refman/5.0/en/date-calculations.htm l

Regards,
Bill K.