James Bensley wrote:
I am trying to execute the following MySQL statement and am coming up a croppa' SELECT name FROM `visitor` WHERE dob = DATE_SUB(CURDATE(), INTERVAL 21 YEAR)
This is to select everyone from a table who is 21 years old, just as an example. The statement runs and returns with no errors but returns no entries (and there are entries that are 21 years old) Do people think the problem is in my statement or my database?
Possibly because that statement will only match dates that are /exactly/ 21 years ago - i.e. to the day:
mysql> select DATE_SUB(CURDATE(), INTERVAL 21 YEAR); +---------------------------------------+ | DATE_SUB(CURDATE(), INTERVAL 21 YEAR) | +---------------------------------------+ | 1989-04-06 | +---------------------------------------+
You probably just want to modify it so that the results match a range greater than [exactly] 21 years ago and less than [exactly] 22 years ago, e.g.
SELECT name FROM `visitor` WHERE dob > DATE_SUB(CURDATE(), INTERVAL 21 YEAR) and dob < DATE_SUB(CURDATE(), INTERVAL 22 YEAR)
or something like that - I'm sure there's a neater way to do it!
Cheers, Simon