聚合函数是比较where、having 的关键。
开门见山。where、聚合函数、having 在from后面的执行顺序:
where>聚合函数(sum,min,max,avg,count)>having
列出group by来比较二者。()因where和having 在使用group by时用的最多)
若须引入聚合函数来对group by 结果进行过滤 则只能用having。(此处不多说,自己想 是先执行聚合函数还是先过滤 然后比对我上面列出的执行顺序 一看便知)
样例:select sum(score) from student where sex='man' group by name having sum(score)>210
注意事项 :
1、where 后不能跟聚合函数,因为where执行顺序大于(优先于)聚合函数。
2、where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。
3、having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。
实践出真知,让我们进行具体的实验!!!!!
测试场景说明 : 查询 某学生 的 平均成绩
CREATE TABLE `test_having_where` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name_index` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=151868 DEFAULT CHARSET=utf8
package com.test.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
/**
* Created by szh on 2017/12/26.
*
* @author szh
* @date 2017/12/26
*/
public class CreateDemoData {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
Boolean result = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 通过驱动管理类获取数据库链接
connection = DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/td_dev?serverTimezone=Asia/Shanghai&characterEncoding=utf-8",
"root", "123456");
// 定义sql语句 ?表示占位符
String sql = "INSERT INTO test_having_where (`name`, type, score) VALUES (?, ?, ?)";
preparedStatement = connection.prepareStatement(sql);
String[] course = {"english", "chinese", "java", "css", "html"};
Random random = new Random();
for (int i = 0; i < 30000; i++) {
StringBuffer tmpName = new StringBuffer();
for (int m = 0; m < 8; m++) {
int key = random.nextInt() % 26;
char s = (char) ('a' + key);
tmpName.append(new Character(s));
}
preparedStatement.setString(1, tmpName.toString());
for (int j = 0; j < course.length; j++) {
preparedStatement.setString(2, course[j]);
preparedStatement.setInt(3, Math.abs(random.nextInt() % 101));
// 向数据库发出sql执行查询,查询出结果集
result = preparedStatement.execute();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
// if (resultSet != null) {
// try {
// resultSet.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}