mysql的5中查询子句之三having条件查询
程序员文章站
2022-07-13 16:08:16
...
where条件查询和having条件查询的区别
where条件查询的作用域是针对 表 进行操作,而having条件查询则是将 查询结果 进行操作
这是两者对显著的区别
就拿我在【mysql的5中查询子句之二group by分组查询,
链接 http://blog.csdn.net/m0_37886429/article/details/73276996 】中的第9个例子再进一步扩展
1、查询出每个功能id比项目id低1000的栏目
mysql> select functionid,itemid,function,itemid - functionid as low from functions having low < 1000;
备注:如果我们将此处的having换成where就会报错
从上述对比中,可以很清楚的分析出where条件查询和having条件查询的区别,
查看表结构的sql语句为:desc tableName
2、查询出avg功能里面功能id比项目id低1000的
mysql> select functionid,itemid,function,itemid - functionid as low from functions where function = 'avg' having low < 1000;
3、设有成绩表stu,建表语句如下
create table stu (
name varchar(10) not null,
subject varchar(10) not null,
score smallint unsigned not null
) charset utf8;
insert into stu values
('张三','数学',90),
('张三','语文',50),
('张三','地理',40),
('李四','语文',55),
('李四','政治',45),
('王五','化学',30);
查询两门及以上不及格同学的平均分?
思路:先算每个人平均分,再看每个人的挂科情况,最后再统计挂科大于2门以上人的平均分
mysql> select name,avg(score) from stu group by name;
mysql> select name,sum(score < 60) from stu group by name;
mysql> select name,sum(score < 60) as fail ,avg(score) as average from stu group by name having fail >= 2;
mysql的5中查询子句
mysql的5中查询子句之一where条件查询
http://blog.csdn.net/m0_37886429/article/details/73224579
mysql的5中查询子句之二group by分组查询
http://blog.csdn.net/m0_37886429/article/details/73276996
mysql的5中查询子句之三having条件查询
http://blog.csdn.net/m0_37886429/article/details/73323970
mysql的5中查询子句之四order by排序查询
http://blog.csdn.net/m0_37886429/article/details/73457866
mysql的5中查询子句之五limit限制查询
http://blog.csdn.net/m0_37886429/article/details/73481387