我经常使用以下查询,但我很确定必须有更有效的方法来做到这一点。
基本上,我从用户表中计算人们出生的几十年:
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'))