如何建设局域网内部网站,wordpress 慢的上天,网络整合营销4i原则,网络营销的基本流程1、having关键字概览
1.1、作用
对查询的数据进行筛选
1.2、having关键字产生的原因
使用where对查询的数据进行筛选时#xff0c;where子句中无法使用聚合函数#xff0c;所以引出having关键字
1.3、having使用语法
having单独使用#xff08;不与group by一起使用where子句中无法使用聚合函数所以引出having关键字
1.3、having使用语法
having单独使用不与group by一起使用在Oracle中会报错单独使用时大部分场合与where相同having与group by一起使用这是having关键字产生的初衷对分组之后的数据再进行筛选
1.4、having与where的区别
一般情况下where用于过滤数据行而having用于过滤分组能用where的地方不要使用havingwhere中不能出现聚合函数而having可以使用聚合函数作为条件where在数据分组前进行过滤而having在数据分组后进行过滤因此where效率一般比having高where是数据从磁盘读入内存时筛选而having是在内存中筛选where是对数据库文件过滤过滤条件是表中的字段而having是对select中查询的字段进行过滤where子句中不能使用字段别名而having子句中可以使用字段别名多表关联查询时where先筛选再联接having先联接再筛选
2、having案例
初始化表以student表为例
create table if not exists student
(id int null,name varchar(50) null,age int null,sex varchar(2) null,score double null
)
comment 学生表;INSERT INTO student (id, name, age, sex, score) VALUES (1, 张三, 18, 男, 70);
INSERT INTO student (id, name, age, sex, score) VALUES (2, 李四, 17, 男, 60);
INSERT INTO student (id, name, age, sex, score) VALUES (3, 王五, 19, 男, 80);
INSERT INTO student (id, name, age, sex, score) VALUES (4, 赵六, 16, 男, 90);
INSERT INTO student (id, name, age, sex, score) VALUES (5, 七七, 16, 女, 95);
INSERT INTO student (id, name, age, sex, score) VALUES (6, 九九, 17, 女, 85);
INSERT INTO student (id, name, age, sex, score) VALUES (7, 十一, 18, 女, 80);
INSERT INTO student (id, name, age, sex, score) VALUES (8, 小明, 19, 男, 90);
INSERT INTO student (id, name, age, sex, score) VALUES (9, 小军, 17, 男, 55);
INSERT INTO student (id, name, age, sex, score) VALUES (10, 小雷, 19, 女, 60);
2.1、having单独使用
案例1查询学生表中成绩在80分以上的数据
select * from student having score 80
等同于
select * from student where score 80
having使用的错误
select id,name,age
from student
having score 80 -- 报错score筛选条件没有出现在select中where使用的错误
selectid,name,age,score as fenshu
from student
where fenshu 80 -- 报错where子句中不能使用字段别名
2.2、having与group by一起使用
案例2求各个年龄段的平均分和年龄
select age,avg(score) from student group by age
如下 案例3求学生平均分大于80分的年龄段及平均分
这里只能使用having对平均分进行筛选使用where会报错
selectage,avg(score)
from student
group by age
having avg(score) 80
-- 结果为16岁
案例4查询学生年龄平均分大于80分中男生的信息姓名男生的分数
selectname,sex,age,score
from student
where sex 男
group by name,sex,age,score
having avg(score) 80结果