Calculating age in mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ozchadl
    New Member
    • Apr 2010
    • 26

    #1

    Calculating age in mysql

    I have a table called 'Animals' in a mysql database.
    The columns / fields are:
    'name'
    'animal'
    'year born'

    The 'year born' field / column only contains a year such as 2000

    I need help for these 2 items:
    1) I am trying to calculate the animals age.

    2) for a different query I am trying to calculate the animals age in human years, which is by multiplying the animals age by 3


    Thanks for your help
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Why are you trying to do this all in the database? MySQL is mainly for storing data which can be called with other languages and used.

    If you are using PHP, you can simply call that year and then subtract it from this year.

    Why don't you have a date column instead of your year column, which I presume is just an INT?

    If you change that to DATE (so you have day and month as well as year) then you can use some of MySQL date functions.

    Comment

    • ozchadl
      New Member
      • Apr 2010
      • 26

      #3
      I would prefer not to alter the field / column name and any data.
      I know that it is possible to do this age calculation in mysql.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        I still don't understand what you want? Do you want a MySQL output on ages?

        Code:
        SELECT (YEAR(CURRENT_DATE)-yearBorn) WHERE ...

        Comment

        • ozchadl
          New Member
          • Apr 2010
          • 26

          #5
          I got it to finally work
          This is part of the code that I used, which works to calculate the age

          left(curdate(), 4) - (year born)

          Comment

          Working...