3

I have mysql table people with row "born". In born I have

born
----
2013-07-15
2010-11-29
2013-11-01

etc. How can I make mysql query to select people older than 3 months so it should compare row "born"? in this case it should return first and second item.

And how to select people younger than 4 years? in this case it should return all items. I would like to port it so select older/younger than day, x days, month, months, year, years etc.

many thanks

peter
  • 4,289
  • 12
  • 44
  • 67
  • Asked and answered: http://stackoverflow.com/questions/288984/the-difference-in-months-between-dates-in-mysql you may have to use datediff if you want months. – xQbert Nov 15 '13 at 15:10

1 Answers1

7

This isn't exactly correct, because it won't account for birthdays, but should give you the basic idea:

older than 3 months:

SELECT *
FROM yourtable
WHERE born < (now() - INTERVAL 3 MONTH)

younger than 4 years:

SELECT *
FROM yourtable
WHERE born > (now() - INTERVAL 4 YEAR)
Marc B
  • 356,200
  • 43
  • 426
  • 500