当前位置:网站首页>Usage of mysql having
Usage of mysql having
2022-07-31 23:21:00 【ximeneschen】
一、 having的用法
having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前.而 having子句在聚合后对组记录进行筛选.
二、SQL实例
- 显示每个地区的总人口数和总面积.
SELECT region, SUM(population), SUM(area) FROM bbc GROUP BY region
先以region把返回记录分成多个组,这就是GROUP BY的字面含义.分完组后,然后用聚合函数对每组中的不同字段(一或多条记录)作运算.
- 显示每个地区的总人口数和总面积.仅显示那些面积超过1000000的地区.
//在这里,我们不能用where来筛选超过1000000的地区,因为表中不存在这样一条记录.相反,having子句可以让我们筛选成组后的各组数据
SELECT region, SUM(population), SUM(area) FROM bbc GROUP BY region HAVING SUM(area)>1000000
- mysql判断某个字段的长度:
select home_page from aaa表 where char_length(trim(home_page))<10 and char_length(trim(home_page))>1;
三、mysql中的where和having子句的区别
- mysql中的where和having子句
The function of filtering records can be realized,But there are some differences in their usage.
用group by和havingClause union to find distinct records,sql如下:
select uid,email,count(*) as ct from `edm_user081217` GROUP BY email
然后看这个,就容易理解了
//先用group by 对email进行分组,在用havingto filter greater than1的,This finds out duplicate records
select uid,email,count(*) as ct from `edm_user081217` GROUP BY email HAVING ct > 1
- 以下是having和where的区别:
Select city FROM weather WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
- 作用的对象不同.
- WHERE 子句作用于表和视图
- HAVING 子句作用于组.
WHERE 在分组和聚集计算之前选取输入行(因此,它控制哪些行进入聚集计算), 而 HAVING 在分组和聚集之后选取分组的行.因此,WHERE 子句不能包含聚集函数; Because it doesn't make sense to try to use an aggregate function to determine which rows are input to the aggregate operation. 相反,HAVING 子句总是包含聚集函数.(严格说来,You can write without using aggregation HAVING 子句, But doing so is in vain.The same conditions can be used more effectively WHERE 阶段.)
在前面的例子里,我们可以在 WHERE City name restrictions apply,Because it doesn't require aggregation. 这样比在 HAVING It is more efficient to increase the limit here,Because we avoid for those that don't pass WHERE Examine the rows for grouping and aggregation calculations
综上所述:
- having一般跟在group by之后,Performs part of the record group selection to work.
- whereIt is the execution of all data to work.
- 再者havingAggregate functions can be used,如having sum(qty)>1000
边栏推荐
- Recognize anomalies (you will understand after reading this)
- 【ACM】2022.7.31训练赛
- 2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
- Shell常用脚本:Nexus批量上传本地仓库脚本
- 字符编码和浮点型计算精度丢失问题
- Difference Between Stateless and Stateful
- 硬件设备计算存储及数据交互杂谈
- (26)Blender源码分析之顶层菜单的关于菜单
- HTC使用官方固件作为底包制作rom卡刷包教程
- [QNX Hypervisor 2.2 User Manual]9.14 set
猜你喜欢

Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)

Collation of knowledge points in Ningbo University NBU IT project management final exam

Interview assault 69: TCP reliable?Why is that?

逐步手撕轮播图3(保姆级教程)

2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法

一文带你了解 Grafana 最新开源项目 Mimir 的前世今生

Write a database document management tool based on WPF repeating the wheel (1)

Unity - LineRenderer show a line
I don't know what to do with sync issues

消息队列消息存储设计(架构实战营 模块八作业)
随机推荐
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none failed
HTC使用官方固件作为底包制作rom卡刷包教程
Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
10大主流3D建模技术
SQL injection Less42 (POST type stack injection)
Dry goods | 10 tips for MySQL add, delete, change query performance optimization
The article you worked so hard to write may not be your original
Bionic caterpillar robot source code
一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
(26)Blender源码分析之顶层菜单的关于菜单
标段参数说明
高等代数_证明_任何矩阵都相似于一个上三角矩阵
SQL注入 Less54(限制次数的SQL注入+union注入)
IDA PRO中汇编结构体识别
20. Support vector machine - knowledge of mathematical principles
面试突击69:TCP 可靠吗?为什么?
lua入门案例实战123DIY
Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)