当前位置:网站首页>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
边栏推荐
- UOS统信系统 - WindTerm使用
- Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
- Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
- Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
- Quick Start Tutorial for flyway
- Shell常用脚本:Nexus批量上传本地仓库脚本
- leetcode:126. 单词接龙 II
- [QNX Hypervisor 2.2 User Manual]9.14 set
- [QNX Hypervisor 2.2 User Manual]9.16 system
- How to get useragent
猜你喜欢

VOT2021比赛简介

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

基于单片机GSM的防火防盗系统的设计

Bionic caterpillar robot source code

(26)Blender源码分析之顶层菜单的关于菜单

新产品如何进行网络推广?

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

Drawing process of hand-drawn map of scenic spots

(26) About menu of the top menu of Blender source code analysis

消息队列存储消息数据的MySQL表格
随机推荐
SQL injection Less42 (POST type stack injection)
20. Support vector machine - knowledge of mathematical principles
What is customer profile management?
Unity - by casting and cloning method dynamic control under various UGUI create and display
LevelSequence source code analysis
嵌入式开发没有激情了,正常吗?
SQL injection Less47 (error injection) and Less49 (time blind injection)
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
Judging decimal points and rounding of decimal operations in Golang
Unity-LineRenderer显示一条线
Unity - LineRenderer show a line
VOT2021比赛简介
Write a database document management tool based on WPF repeating the wheel (1)
了解下C# 匿名方法
EntityFramework保存到SQLServer 小数精度丢失
【ACM】2022.7.31训练赛
How to get useragent
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
支付模块实现
Flutter教程之四年开发经验的高手给的建议