[英]How to determine the difference between two dates in years and months in MYSQL?
The database holds records for people of all ages including children under the age of 1 where months are important. 该数据库保存着各个年龄段的记录,其中包括1个月以下重要的1岁以下的孩子。
Using 使用
SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age;
The problem you get is anyone < 1 year shows up as 0; 您遇到的问题是,任何<1年的人都显示为0;
Using 使用
SELECT TIMESTAMPDIFF(MONTH, dob, CURDATE()) AS age;
Anyone over the age of 1 just shows up in months so a 43 year old will have an age of > 500 months 1岁以上的任何人都只会在几个月内露面,因此43岁的年龄将大于500个月
Using 使用
SELECT TIMESTAMPDIFF(MONTH, dob, CURDATE()) / 12 AS age;
You can get some results like .25 for a 3 month old or 43.25 for someone who is > 43 您可以获得一些结果,例如3个月大的.25或43岁以上的人的43.25
What I am trying to achieve is something like 我想要达到的目标是
How can something like this be expressed in a mysql query? 怎样在mysql查询中表达这样的东西?
SELECT IF(TIMESTAMPDIFF(MONTH, dob, CURDATE())>12,TIMESTAMPDIFF(MONTH, dob, CURDATE())/12+ 'yeasrs old' , TIMESTAMPDIFF(MONTH, dob, CURDATE())+'months old')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.