当前位置:网站首页>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
边栏推荐
- 游戏安全03:缓冲区溢出攻击简单解释
- hboot与recovery、boot.img、system.img
- Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
- [QNX Hypervisor 2.2用户手册]9.14 set
- Audio alignment using cross-correlation
- Daily--Kali opens SSH (detailed tutorial)
- 基于mysql的消息队列设计
- 【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
- Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
- server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none failed
猜你喜欢
I don't know what to do with sync issues
(26) About menu of the top menu of Blender source code analysis
Shell常用脚本:Nexus批量上传本地仓库脚本
Payment module implementation
Design of Fire and Anti-theft System Based on Single Chip GSM
MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
Handwritten a simple web server (B/S architecture)
Flex layout in detail
逐步手撕轮播图3(保姆级教程)
支付模块实现
随机推荐
I don't know what to do with sync issues
Write a database document management tool based on WPF repeating the wheel (1)
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
lua入门案例实战123DIY
【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
SQL注入 Less47(报错注入) 和Less49(时间盲注)
一文概述:VPN的基本模型及业务类型
如何减少软件设计和实现之间鸿沟
SQL injection Less54 (limited number of SQL injection + union injection)
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
#yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
Shell常用脚本:Nexus批量上传本地仓库脚本
如何导入 Golang 外部包并使用它?
SQL注入 Less38(堆叠注入)
(26) About menu of the top menu of Blender source code analysis
LeetCode 第 304 场周赛
20. Support vector machine - knowledge of mathematical principles
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
"APIO2010" Patrol Problem Solution
SQL injection Less38 (stack injection)