mysql - How to count by current age given DOB and registration date sql -
i have users table includes: userid, username, dob, registrationdate. count members current age members registered since 2000. know latter part of query struggle counting part:
select count (dob) registration_date >= ‘01/01/2000’
any help. thanks
you need group member ages need calculate age in group clause. complicated because need account leap years. (i borrowed leap year part here.)
create table users (userid int, username varchar(10), dob date, registrationdate date); insert users values (1,'bob','1990-06-01','2005-06-01') ,(1,'old gus','1990-06-01','1995-06-01') ,(1,'joe','1991-01-01','2010-06-01') ,(1,'jim','1950-01-01','2010-06-01') select year(curdate()) - year(dob) - (date_format(curdate(), '%m%d') < date_format(dob, '%m%d')) ,count(1) users registrationdate >= '01/01/2012' group year(curdate()) - year(dob) - (date_format(curdate(), '%m%d') < date_format(dob, '%m%d'))
see sqlfiddle.
Comments
Post a Comment