Will
Will

Reputation: 2152

Add year to column before compare in SQL query

I am querying a MySQL database and I need to add a year to a column (of type date) before the compare operation.

I would expect is to look something like this:

SELECT count(*) AS count 
  FROM users 
 WHERE renewed + 1 year < '2009-12-12'

Upvotes: 0

Views: 432

Answers (2)

Pan Thomakos
Pan Thomakos

Reputation: 34350

You can use the mysql DATE_ADD function:

DATE_ADD(renewed, INTERVAL 1 YEAR)

Upvotes: 1

OMG Ponies
OMG Ponies

Reputation: 332631

Use:

SELECT COUNT(*) AS count 
  FROM USERS u 
 WHERE DATE_ADD(u.renewed, INTERVAL 1 YEAR) < '2009-12-12'

Reference:

Upvotes: 3

Related Questions