当前位置:网站首页>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
边栏推荐
- 手写一个简单的web服务器(B/S架构)
- TestCafeSummary
- Fixed-length usage of nanopb string type based on RT1052 Aworks (27)
- ICML2022 | 深入研究置换敏感的图神经网络
- Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
- Interview assault 69: TCP reliable?Why is that?
- 【读书笔记->数据分析】02 数据分析准备
- The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
- cobaltstrike
- 面试突击69:TCP 可靠吗?为什么?
猜你喜欢
Payment module implementation
《ArchSummit:时代的呐喊,技术人听得到》
数据分析(一)——matplotlib
消息队列消息存储设计(架构实战营 模块八作业)
Drawing process of hand-drawn map of scenic spots
How to identify fake reptiles?
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
A high-quality WordPress download site template theme developed abroad
如何减少软件设计和实现之间鸿沟
Judging decimal points and rounding of decimal operations in Golang
随机推荐
一款国外开发的高质量WordPress下载站模板主题
基于mysql的消息队列设计
Bionic caterpillar robot source code
How to get useragent
In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
(26) About menu of the top menu of Blender source code analysis
@JsonFormat(pattern="yyyy-MM-dd") time difference problem
MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
UOS - WindTerm use
#yyds干货盘点# 面试必刷TOP101:链表中环的入口结点
「APIO2010」巡逻 题解
Binary tree non-recursive traversal
消息队列消息存储设计(架构实战营 模块八作业)
如何减少软件设计和实现之间鸿沟
/etc/resolv.conf的作用
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
Write a database document management tool based on WPF repeating the wheel (1)
Golang - from entry to abandonment
SQL注入 Less42(POST型堆叠注入)
浏览器下载快捷方式到桌面(PWA)