我使用了以下查询,但很确定必须有更有效的方法来执行此操作。
基本上,我在用户表中计算人们出生的几十年:
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'))
答案 0 :(得分:6)
您可以计算十年并使用它来对用户进行分组:
SELECT
COUNT(id) AS howmany,
CEIL((`yyyy`+1)/10) AS decade,
yyyy
FROM `bday`
GROUP BY decade
答案 1 :(得分:0)
计算人们出生的几十年:
select substring(yyyy, 1, 3) || '0''s' decade, count(*) howmany
from bday
group by decade