当前位置:网站首页>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
边栏推荐
- 逐步手撕轮播图3(保姆级教程)
- Flutter教程之 01配置环境并运行demo程序 (教程含源码)
- 【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
- Payment module implementation
- 如何导入 Golang 外部包并使用它?
- Components of TypeScript
- Golang - from entry to abandonment
- Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
- TestCafeSummary
- 「SDOI2016」征途 题解
猜你喜欢

How to identify fake reptiles?

SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发

消息队列消息存储设计(架构实战营 模块八作业)

Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios

Network security - crack WiFi through handshake packets (detailed tutorial)

【Acwing】第62场周赛 题解

支付模块实现

消息队列存储消息数据的MySQL表格

Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决

逐步手撕轮播图3(保姆级教程)
随机推荐
基于单片机GSM的防火防盗系统的设计
JS basic exercises
如何减少软件设计和实现之间鸿沟
一款国外开发的高质量WordPress下载站模板主题
博弈论(Depu)与孙子兵法(42/100)
周总结
Fixed-length usage of nanopb string type based on RT1052 Aworks (27)
PHP三元(三目)运算符
数据分析(一)——matplotlib
逐步手撕轮播图3(保姆级教程)
Components of TypeScript
MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
「APIO2010」巡逻 题解
SQL注入 Less47(报错注入) 和Less49(时间盲注)
SQL injection Less38 (stack injection)
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
Document management and tools in the development process
HTC using official firmware as bottom bag made ROM brush card bag tutorial
leetcode:126. 单词接龙 II
Design of Fire and Anti-theft System Based on Single Chip GSM