在MySQL中进行筛选时,必然会遇到where或者having
1、where和having都可以对数据库中查询的内容进行筛选,在其后写筛选条件。
2、where只能对行内容进行筛选
例如查询学生表中 成绩大于50分的人
select student_name, student_grade from student where student_grade > 50
3、having可以实现所有where的功能,并且可以对分组进行筛选。
where不能对分组进行筛选,比如筛选出每组人数大于5人的分组
这个时候就不能使用where,只能用having。
select count(student) as student_num, student_grade from student group by student_grade having student_num > 5;
不建议用having替代where。
4、where和having可以同时使用,where必须在groupby之前,having必须在groupby之后,where筛选的条件会影响having的结果。
可以理解为,数据库先由where进行筛选,结果再由groupby分组,然后才是having筛选。