var sidebar_align = 'right';
var content_container_margin = parseInt('290px');
var sidebar_width = parseInt('270px');
Unanswered: Age Calculations
I have a table of Users on a MySQL database, which includes a field, BirthDate.
I would like to be able to:
- Calculate the User's age, based on the BirthDate, to return in a query, and
- Filter by that calculated age (i.e.: Find everyone from 18-25).
I have found various examples of returning the age in a query, but I haven't found the best way to filter by age, age range, etc.
A HAVING clause should work. The following is an expansion of the PET tutorial example in the msyql manual (untested but should work) -
SELECT name, birth, CURDATE(),
(YEAR(CURDATE())-YEAR(birth)) - (RIGHT(CURDATE(),5)<RIGHT(birth,5))
HAVING age >= 18 AND age <= 25
You could also put the age calculation into a WHERE clause with the comparisons.
Thanks. I originally tried it using a WHERE clause on the age alias, and it didn't work. I tried your example with the HAVING clause, and it did the trick.
do you undestand what this is doing? --
if not, i would suggest that you re-write it using a CASE expression, because it won't work in any other database written like that
... - (RIGHT(CURDATE(),5)<RIGHT(birth,5))