This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing the easy and finest way for calculating Age from Date of Birth of MySQL.
As a database architecture, I never suggest to store calculated Age into database because this is ever changing information base on time.
We should store only the Date of Birth into our database system and whenever it requires to show Age, we should calculate Age using Date of Birth.
I have found many complex solutions to calculate Age, but here I am sharing one of the easiest way.
First, create a table with sample data:
1 2 3 4 5 6 7 8 9 10 |
CREATE TABLE tbl_UserAge ( UserID INTEGER ,DOB DATE ); INSERT INTO tbl_UserAge VALUES (1,'19880126'),(2,'19930408'),(3,'19860818') ,(4,'19900316'),(5,'19870607'),(6,'19850215'); |
Calculating Age for all data:
1 2 3 4 5 |
SELECT UserID ,DOB ,TIMESTAMPDIFF(YEAR,DOB,CURDATE()) AS UserAge FROM tbl_UserAge; |