mysql - Get all ages count on a table -


my goal how many users got on database per year. save birthday in timestamp. after reading solution how count 2 different data in 1 query

makes me wonder, if oldest member have 120 old, have write 120 cases each count "between" 2 timstamps defines year?

that sounds bit odd, there's no other way job done?

desired result :

age 1 - 0 people

age 2 - 1 people

.

.

.

age 20 - 314 people

age 21 - 30 people

.

.

.

age 25 - 47 people

.

.

age 120 - 1 people

you don't want 1 query each age. assuming have query can calculate age nearest year, can count with

select (..expression..) "age", count(*) users group "age" 

if you're looking the (..expression..) compute ages, want close to

extract(year current_date()) - extract(year birthdate) - if(date_format(current_date(),"%c%d") < date_format(birthdate,"%c%d"),1,0) 

but there other (probably more efficient) versions might prefer.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -