View Single Post

   
  #2 (permalink)  
Old 02-28-2008, 08:52 AM
Captain Paralytic
 
Posts: n/a
Default Re: Trouble using SYSDATE()


b.coolsaet@gmail.com wrote:

> Hi,
>
> I have to select elements that are not older than 2 months (=60 days).
> The table has a standard DATE column with format (YYYY-MM-DD), called
> 'date'.
>
> I tried a lot of queries, one of them is following:
>
> SELECT * FROM mytable WHERE date > (SYSDATE()-60);
>
> So basically i'm telling mysql to select elements where the date is
> bigger (= older) than the actual system date diminished with 60 days.
> But it does't return anything (and yes I have elements newer than 2
> months in the table :-)
>
> I suppose something is wrong with the use of SYSDATE. Anyone any
> experience with this ?


The user manual has a lot of experience of this and explains how to do
it.

Think about it. SYSDATE() "Returns the current date and time as a value
in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether
the function is used in a string or numeric context." (quoted from
manual)

If you simply substract 60 from this, how is MySQL to know 60 what?
60 years?
60 seconds?
60 bars of chocolate?

Try:
SELECT * FROM mytable WHERE date > (SYSDATE()-INTERVAL 60 DAY);

as the manual suggests
http://dev.mysql.com/doc/refman/5.0/...functions.html

Reply With Quote