More efficient way to run date request / yyyy?

I use the following query quite a bit, but I'm sure there should be a more efficient way to do this.

Basically, I believe that for a decade people were born from a user table:

select count(*) as howmany, yyyy from bday where (((yyyy > '1949') AND (yyyy < '1961')) AND (user_id = '63')) UNION select count(*) as howmany, yyyy from bday where (((yyyy > '1959') AND (yyyy < '1971')) AND (user_id = '63')) UNION select count(*) as howmany, yyyy from bday where (((yyyy > '1969') AND (yyyy < '1981')) AND (user_id = '63')) UNION select count(*) as howmany, yyyy from bday where (((yyyy > '1979') AND (yyyy < '1991')) AND (user_id = '63')) UNION select count(*) as howmany, yyyy from bday where (((yyyy > '1989') AND (yyyy < '2001')) AND (user_id = '63')) 
+6
source share
2 answers

You can calculate a decade and use it to group users:

 SELECT COUNT(id) AS howmany, CEIL((`yyyy`+1)/10) AS decade, yyyy FROM `bday` GROUP BY decade 
+6
source

to count the decades in which people were born:

 select substring(yyyy, 1, 3) || '0''s' decade, count(*) howmany from bday group by decade 
0
source

Source: https://habr.com/ru/post/916149/


All Articles