我经常使用以下查询,但可以肯定必须有一种更有效的方法来执行此查询。
基本上,我从一个用户表中计算出人们出生的几十年:
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'))
最佳答案
您可以计算十年并将其用于对用户进行分组:
SELECT
COUNT(id) AS howmany,
CEIL((`yyyy`+1)/10) AS decade,
yyyy
FROM `bday`
GROUP BY decade
关于mysql - 运行日期/年查询的更有效方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10663637/