开发者

Get Age of a person in MySQL

开发者 https://www.devze.com 2023-01-01 13:57 出处:网络
How can i get a Persons age in mysql Imagine i have a table with member.id, member.month, member.year

How can i get a Persons age in mysql

Imagine i have a table with member.id, member.month, member.year Now i need to get age of a member in Months.

Say:

member.month = 1 开发者_如何学Cand member.year = 2000 

That would mean the age of this member is 126 because 10 Years equals 120 Months and 6 Months is 6 Months. Now the result is that the members age is 126 Month.

How can i do it in MySql


select YEAR(NOW())*12+MONTH(NOW()) - (member.year*12+member.month) +1;

To make my answer yield 126, I had to add 1 to the expression. I'm not sure that makes sense, but it just depends on how you want to define age.


  1. it shouldn't be separate fields, it must be one field of date type.

  2. mysql has good date calculations example page: http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html
    first one is yours

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号