alvin567 Posted July 17, 2012 Share Posted July 17, 2012 I have three fields in the database for a date of birth, day | month | year Output the date type If possible I want to do the manipulation in the database and output the age. Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 my point is I want to output the ages. Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 17, 2012 Share Posted July 17, 2012 Why would you use 3 seperate columns when you can use a single DATE column? Furthermore, if you do use a date column you can utilize built-in mysql functions to calculate age. See http://www.gizmola.com/blog/archives/107-Calculate-a-persons-age-in-a-MySQL-query.html Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 gizmo,there are three of these columns in the database,I am trying to use mysql queries, can i cast these 3 columns as date type instead? Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 17, 2012 Share Posted July 17, 2012 gizmo,there are three of these columns in the database,I am trying to use mysql queries, can i cast these 3 columns as date type instead? Yes, you can manufacture a mysql date using TO_DATE() Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 gizmo, I am thinking of strictly using mysql to manipulate the data. Currently my environment is php/mysql Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 17, 2012 Share Posted July 17, 2012 I meant to say: STR_TO_DATE() Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 ok,so i use a str_to_date() then how do I calculate the age? Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 17, 2012 Share Posted July 17, 2012 ok,so i use a str_to_date() then how do I calculate the age? Did you read the blog article? Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 SELECT *, STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', MONTH(birthdate), '-', DAY(birthdate)) ,'%Y-%c-%e') AS birthday FROM user; I think you mean this portion? Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 Gizmo: I have manage to solve the issue already,thanks SELECT * , YEAR( CURDATE( ) ) - year - IF( STR_TO_DATE( CONCAT( YEAR( CURDATE( ) ) , '-', month, '-', day ) , '%Y-%c-%e' ) > CURDATE( ) , 1, 0 ) AS age FROM member LIMIT 0 , 300; Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 17, 2012 Share Posted July 17, 2012 Great. There are numerous ways to calculate the age. If you want to simplify your query a bit you can use this as well: SELECT *, FLOOR(DATEDIFF(CURRENT_DATE, STR_TO_DATE(CONCAT(year, '-', month, '-', day), '%Y-%c-%e'))/365.25) as age FROM member Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 but I though datediff is for mssql only? Quote Link to comment Share on other sites More sharing options...
ignace Posted July 17, 2012 Share Posted July 17, 2012 Read the manual: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html DATEDIFF() is a mysql function. Quote Link to comment Share on other sites More sharing options...
alvin567 Posted July 17, 2012 Author Share Posted July 17, 2012 I have a questions: how do I count the number of values in a database using a group by conditions? Quote Link to comment Share on other sites More sharing options...
ignace Posted July 17, 2012 Share Posted July 17, 2012 SELECT col, COUNT(col) FROM table GROUP BY col Quote Link to comment Share on other sites More sharing options...
Barand Posted July 17, 2012 Share Posted July 17, 2012 Mysql also has a COUNT() function - not just M$SQL Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.