SQL集合函数中case when then 使用技巧
2016-01-14 10:27
681 查看
假设数据库有一张表名为student的表。
![](http://files.jb51.net/upload/201109/20110909092702262.jpg)
如果现在要你根据这张表,查出江西省男女个数,广东省男生个数,浙江省男女个数 怎么写SQL语句?即要生成下结果表
![](http://files.jb51.net/upload/201109/20110909092702243.jpg)
答案是:select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex
count()函数即根据给定的范围和group by(统计方式) 而统计行数据的条数
我们一步步来理解上面语句
1. select sex from student (查询数据表中的存在的男女条数)
![](http://files.jb51.net/upload/201109/20110909092702157.jpg)
2.select sex, count (*) as num from student group by sex (查询表中男女数量)
![](http://files.jb51.net/upload/201109/20110909092702625.jpg)
3.select sex ,province, count (*)as num from student group by sex,province (查询各省男女数量)
![](http://files.jb51.net/upload/201109/20110909092702626.jpg)
重点来了,如果我把count(*) 中的 *号换成任一列名呢? 如count(province) 会怎样?
4.select sex ,province, count (province)as num from student group by sex,province (查询各省男女数量)
结果跟上图一样:这说明换不换都一样。又有count (province)等价于 count(case province when '浙江省' then '浙江省' else province end )
但是如果我们缩小范围呢即count(case province when '浙江省' then '浙江省' end ) 那么请看下面
5.select sex ,province, count ( case province when '浙江省' then '浙江省' end )as num from student group by sex,province
![](http://files.jb51.net/upload/201109/20110909092702410.jpg)
即统计男女数量范围限定在浙江省 再精简一下即下面
6.select sex, count ( case province when '浙江省' then '浙江省' end ) as 浙江省 from student group by sex
![](http://files.jb51.net/upload/201109/20110909092702763.jpg)
已经接近我们的要求了,现在只要加上另几个字段就是了
7.select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex
![](http://files.jb51.net/upload/201109/20110909092702243.jpg)
小结:当然实现有很多种方法 可以多个子查询拼接起来也不无可厚非。我这只是一种思路
补充:case when then 知识点
(1) select (case province when '浙江省' then '浙江' when '江西省' then '江西' end ) as 省份 from student
![](http://files.jb51.net/upload/201109/20110909092703175.jpg)
如果默认范围如果没全包含则为空 像上图的广东省为空
(2)select (case province when '浙江省' then '浙江' when '江西省' then '江西' else province end ) as 省份 from student
![](http://files.jb51.net/upload/201109/20110909092702262.jpg)
如果现在要你根据这张表,查出江西省男女个数,广东省男生个数,浙江省男女个数 怎么写SQL语句?即要生成下结果表
![](http://files.jb51.net/upload/201109/20110909092702243.jpg)
答案是:select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex
count()函数即根据给定的范围和group by(统计方式) 而统计行数据的条数
我们一步步来理解上面语句
1. select sex from student (查询数据表中的存在的男女条数)
![](http://files.jb51.net/upload/201109/20110909092702157.jpg)
2.select sex, count (*) as num from student group by sex (查询表中男女数量)
![](http://files.jb51.net/upload/201109/20110909092702625.jpg)
3.select sex ,province, count (*)as num from student group by sex,province (查询各省男女数量)
![](http://files.jb51.net/upload/201109/20110909092702626.jpg)
重点来了,如果我把count(*) 中的 *号换成任一列名呢? 如count(province) 会怎样?
4.select sex ,province, count (province)as num from student group by sex,province (查询各省男女数量)
结果跟上图一样:这说明换不换都一样。又有count (province)等价于 count(case province when '浙江省' then '浙江省' else province end )
但是如果我们缩小范围呢即count(case province when '浙江省' then '浙江省' end ) 那么请看下面
5.select sex ,province, count ( case province when '浙江省' then '浙江省' end )as num from student group by sex,province
![](http://files.jb51.net/upload/201109/20110909092702410.jpg)
即统计男女数量范围限定在浙江省 再精简一下即下面
6.select sex, count ( case province when '浙江省' then '浙江省' end ) as 浙江省 from student group by sex
![](http://files.jb51.net/upload/201109/20110909092702763.jpg)
已经接近我们的要求了,现在只要加上另几个字段就是了
7.select sex ,count ( case province when '广东省' then '广东省' end )as 广东省 ,count ( case province when '江西省' then '江西省' end )as 江西省 ,count ( case province when '浙江省' then '浙江省' end )as 浙江省 from student group by sex
![](http://files.jb51.net/upload/201109/20110909092702243.jpg)
小结:当然实现有很多种方法 可以多个子查询拼接起来也不无可厚非。我这只是一种思路
补充:case when then 知识点
(1) select (case province when '浙江省' then '浙江' when '江西省' then '江西' end ) as 省份 from student
![](http://files.jb51.net/upload/201109/20110909092703175.jpg)
如果默认范围如果没全包含则为空 像上图的广东省为空
(2)select (case province when '浙江省' then '浙江' when '江西省' then '江西' else province end ) as 省份 from student
![](http://files.jb51.net/upload/201109/20110909092703945.jpg)
相关文章推荐
- MySQL之终端terminal管理数据库、数据表、数据的基本操作
- mysql中selec sum返回null的解决方法
- MySQL创建用户与授权方法
- mysql的学习笔记
- 在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据)
- 数据库连接池的原理
- PostgreSQL的LIMIT子句解析
- mysql字符编码的设置ini
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
- Oracle树查询(查询所有子节点,父节点等等)
- 数据库-表连接
- mysql多行多列合并为一行一列 【转】
- AP:PaymentTerms 的设置&Invoice batch 知识点。
- MySQL5.7多实例自动化部署脚本
- excel数据插入mysql
- MySQL临时表
- hive支持sql大全