tags:

views:

66

answers:

2

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 and 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

+6  A: 
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.

unutbu
Thank you very much this worked for me
streetparade
This works but it still seems awfully silly to store date info this way.
Erik
@Erik: To play devil's advocate here, what he's storing *isn't* a date. If all he's interested in is year and month, then storing a specific day (even if you standardized on the first day of the month) would be adding illegitimate precision.
Adam Robinson
+2  A: 
  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

Col. Shrapnel
While I'd generally agree that it should be a `Date`, there's no technical reason why it "must be". A better tack to take would be to, like Erik, ask why he's doing that.
Adam Robinson
I did. He responded with "I was drunk"
Erik