Get Age of a person in MySQL
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.
it shouldn't be separate fields, it must be one field of
date
type.mysql has good date calculations example page: http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html
first one is yours
精彩评论