Skip to main content
Inspiring
October 15, 2010
Answered

SQL (Mysql) for recordset including only records less than 2 year old

  • October 15, 2010
  • 1 reply
  • 477 views

Hi Folk,

I am trying to select records where the date field is less than 2 years from todays date.

Something like this:

_______________________________________________

SELECT *

FROM notes

WHERE userid = '$userid' AND date > ( NOW() -2 years)

_______________________________________________

Does anyone know how to write that in sql so it works?

Cheers

Dave

This topic has been closed for replies.
Correct answer David_Powers

Sorry, there should be no comma after INTERVAL.

1 reply

David_Powers
Inspiring
October 15, 2010

Use DATE_SUB()

SELECT *

FROM notes

WHERE userid = '$userid' AND date > DATE_SUB(NOW(), INTERVAL, 2 YEAR)

davecheetAuthor
Inspiring
October 15, 2010

Thanks David,

but I get this error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 2 YEAR)' at line 1

David_Powers
David_PowersCorrect answer
Inspiring
October 15, 2010

Sorry, there should be no comma after INTERVAL.