date of birth 2 age
Example of the script: Date of Birth 2 Age
To calculate the current age:
SELECT
IF(
MONTH(NOW()) > MONTH(DoB) || (MONTH(NOW())=MONTH(DoB) && DAY(NOW()) >= DAY(DoB)),
YEAR(NOW()) - YEAR(DoB),
YEAR(NOW()) - YEAR(DoB) - 1
) AS Age
FROM People
To calculate the age given the date in another field:
SELECT
IF(
MONTH(SomeOtherField) > MONTH(DoB) || (MONTH(SomeOtherField)=MONTH(DoB) && DAY(SomeOtherField) >= DAY(DoB)),
YEAR(SomeOtherField) - YEAR(DoB),
YEAR(SomeOtherField) - YEAR(DoB) - 1
) AS Age
FROM People
To calculate the age given a string:
SELECT
IF(
MONTH('2006-01-01') > MONTH(DoB) || (MONTH('2006-01-01')=MONTH(DoB) && DAY('2006-01-01') >= DAY(DoB)),
YEAR('2006-01-01') - YEAR(DoB),
YEAR('2006-01-01') - YEAR(DoB) - 1
) AS Age
FROM People
(This will give you the age of everyone in the People Table, on January the 1st, 2006.
Of course these queries in itself are pretty meaningless, as a resultset with just ages tells you very little. You will probably want to do something like this:
SELECT
FirstName, LastName,
IF(
MONTH(NOW()) > MONTH(DoB) || (MONTH(NOW())=MONTH(DoB) && DAY(NOW()) >= DAY(DoB)),
YEAR(NOW()) - YEAR(DoB),
YEAR(NOW()) - YEAR(DoB) - 1
) AS Age
FROM People