sql中case when then简单用法

来源:互联网 发布:android耗电量优化 编辑:程序博客网 时间:2024/05/17 23:33

如想统计一栋楼里边各学历的分布情况,即各学历有多少人,degree是学历。下图为数据库:

①普通的写法是

select degree, count(degree) dnumfrom t_residentwhere build_id=1 group by degree;
结果为:

②但若要求得到学历的详细信息,上图就显得不那么直观。可以使用case when then end写法来转化数据库中的信息。

select (casewhen degree=1 then '高中以下'when degree=2 then '高中'when degree=3 then '大专'when degree=4 then '本科'when degree=5 then '硕士'when degree=6 then '博士'else  '博士以上'end)lable,degree, count(degree) dnumfrom t_residentwhere build_id=1 group by degree;
结果为:


这样效果会更好些。


原创粉丝点击