上期我们学习了SQL的基本语句,现在我们再深入了解SQL汇总分析方法。
在实际工作中,我们往往需要对数据进行汇总整理分析,在SQL中我们可以运用函数
进行汇总分析,常用的函数包括:计数count 求和sum 平均值agv 最大数max 最小数 min
具体例子:
![e3f7930d612e3a268a6d6439418982bb.png](https://img-blog.csdnimg.cn/img_convert/e3f7930d612e3a268a6d6439418982bb.png)
![67193e570ab598aeb2b30b516916e332.png](https://img-blog.csdnimg.cn/img_convert/67193e570ab598aeb2b30b516916e332.png)
![333494e030321670a6b15c0c9452affc.png](https://img-blog.csdnimg.cn/img_convert/333494e030321670a6b15c0c9452affc.png)
![964f0bc95837f5c90d51f8efcd7c0239.png](https://img-blog.csdnimg.cn/img_convert/964f0bc95837f5c90d51f8efcd7c0239.png)
![b1347e58fcb1851d4468a5ff5a64cd00.png](https://img-blog.csdnimg.cn/img_convert/b1347e58fcb1851d4468a5ff5a64cd00.png)
![866dd466d2863733219208afde76c051.png](https://img-blog.csdnimg.cn/img_convert/866dd466d2863733219208afde76c051.png)
![b330e49785540f7b2d0119c09b1da468.png](https://img-blog.csdnimg.cn/img_convert/b330e49785540f7b2d0119c09b1da468.png)
![5e1f51be987a2d187c366bf9002ad034.png](https://img-blog.csdnimg.cn/img_convert/5e1f51be987a2d187c366bf9002ad034.png)
![b806dc5fe6d74520bda0c8e52a8cdcc1.png](https://img-blog.csdnimg.cn/img_convert/b806dc5fe6d74520bda0c8e52a8cdcc1.png)
注意:如果需要对函数内对象去重可以直接在前面加上distinct .如果select后面加的是*那就表示是整个表的数据。
分组:
运用 Group by 对数据进行分组处理,在where之后运行
练习:
![c3327460b98252c0be2aaf09f6aa5771.png](https://img-blog.csdnimg.cn/img_convert/c3327460b98252c0be2aaf09f6aa5771.png)
![462401cf279a8cc5360a32caac47b096.png](https://img-blog.csdnimg.cn/img_convert/462401cf279a8cc5360a32caac47b096.png)
![9662481add00f2b50236a4e61f377b37.png](https://img-blog.csdnimg.cn/img_convert/9662481add00f2b50236a4e61f377b37.png)
练习:
对数据先进行分组,然后汇总最后运用条件having进行筛选
![6b7e3623bdda94201c07d6762ddf2a26.png](https://img-blog.csdnimg.cn/img_convert/6b7e3623bdda94201c07d6762ddf2a26.png)
![f30eeb622da2e7ac1b5f208cbd388cda.png](https://img-blog.csdnimg.cn/img_convert/f30eeb622da2e7ac1b5f208cbd388cda.png)
![7b846ac829d41bfdb232d18c997a2b91.png](https://img-blog.csdnimg.cn/img_convert/7b846ac829d41bfdb232d18c997a2b91.png)
![d835368c605bc2d255cb7beec091c71b.png](https://img-blog.csdnimg.cn/img_convert/d835368c605bc2d255cb7beec091c71b.png)
对查询结果进行排序运用gruop by
升序 ASC
降序 DESC
LIMIT跟在后面可以返还指定前几行的数据
null 一般排在最前面
运行顺序在最后
练习:
![92870ee688b5058d517591272cc84768.png](https://img-blog.csdnimg.cn/img_convert/92870ee688b5058d517591272cc84768.png)
![bd2ee48ba339a975226237c743f194a4.png](https://img-blog.csdnimg.cn/img_convert/bd2ee48ba339a975226237c743f194a4.png)
常见报错问题:
from语句中使用了as的别名(from语句先运行,无法识别)
where子句中使用了聚合函数
字串符类型和数值类型比较
sql运行顺序总结:
![41c04908e9d9718526d6b1b763d70f09.png](https://img-blog.csdnimg.cn/img_convert/41c04908e9d9718526d6b1b763d70f09.png)
sql zoo练习:
![a1eba4c8eef85d18a1dcd276405dc73f.png](https://img-blog.csdnimg.cn/img_convert/a1eba4c8eef85d18a1dcd276405dc73f.png)
可以多多做一下sql zoo的练习来巩固知识点。