我有一个mysql表,如下所示。
emp_no emp_name dob gender
1 A 1978-10-10 Male
2 B 1985-02-20 Female
3 C 1982-04-16 Female
4 D 1980-06-27 Male
然后我需要得到以下结果。
age_group No of Male No of Female Total
<25 1 0 1
25-35 0 1 1
35-50 0 1 1
>50 1 0 1
我使用下面的查询来获得这个输出。
SELECT TIMESTAMPDIFF(YEAR, dob, NOW()) AS age_group,
COUNT( IF( TIMESTAMPDIFF(YEAR, dob, NOW()) <25, 1, 0 ) ) AS ag_C,
COUNT( IF( TIMESTAMPDIFF(YEAR, dob, NOW()) BETWEEN 25 AND 35 , 1, 0 ) ) AS ag_B,
COUNT( IF( TIMESTAMPDIFF(YEAR, dob, NOW()) BETWEEN 35 AND 50 , 1, 0 ) ) AS ag_C,
COUNT( IF( TIMESTAMPDIFF(YEAR, dob, NOW()) >50, 1, 0 ) ) AS ag_D
FROM emp GROUP BY age_group
但是我不能得到想要的结果。在这个问题上有人能帮我吗?
4条答案
按热度按时间lmvvr0a81#
用例和子查询
0yg35tkg2#
你可以试试这个:
jk9hmnmh3#
试试这个
zzwlnbp84#
请尝试以下操作: