How to calculate Age by Birthday in Mysql
This article mainly shows you "how to calculate age by birthday in Mysql". The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how to calculate age by birthday in Mysql".
method one
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 AS age
Method 1, the author also points out the flaw, that is, when the date is a future date, the result is 0, not negative; here five functions and two operators are used.
methodology II
SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(birthday, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(birthday, '00-%m-%d')) AS age
Method 2 solves the problem of method 1 being negative, but looks more complex; six functions and three operators are used.
After reading this post, I was dizzy, how can it be so complicated, before using SQL Server is very simple can be. I believe there must be a simple and efficient way. A modified method based on the above method was quickly found.
Improved methods 1 and 2
SELECT year( from_days( datediff( now( ), birthdate)));SELECT YEAR(CURDATE())-YEAR(birthday)-(RIGHT(CURDATE(),5)