在表中,按学历和性别统计平均年龄,当有一个学历的有一个性别没有人时,没人的结果不输出。
SELECT education,gender,AVG(year(now())-year(date_of_birth)) AS 平均年龄 FROM tbl_employees GROUP BY education UNION ALL SELECT education,gender,AVG(year(now())-year(date_of_birth)) AS 平均年龄 FROM tbl_employees GROUP BY gender ORDER BY 平均年龄;
能运行,但显示结果错误。
我尝试了用union all输出全部结果,但还是不对。
让全部结果输出
求指导!
SELECT education,gender,AVG(year(now())-year(date_of_birth)) AS 平均年龄
FROM tbl_employees GROUP BY education,gender
select的非聚合函数字段,都要放到group by 后面
where gender is not null 去除没有值的
SELECT education,gender,AVG(year(now())-year(date_of_birth)) AS 平均年龄
FROM tbl_employees where gender is not null GROUP BY education,gender