MySQL - HAVING 子句



MySQL HAVING 子句

MySQL 的HAVING 子句用于根据条件过滤表中分组的行。

此子句与GROUP BY 子句一起使用,根据一个或多个列对行进行分组,然后根据 HAVING 子句中指定的条件对其进行过滤。因此,HAVING 子句必须始终在 GROUP BY 子句之后。

添加 HAVING 子句到 MySQL 是因为 WHERE 关键字不能与 COUNT()、SUM()、AVG() 等聚合函数一起使用。

此子句类似于 MySQL WHERE 子句。两者之间的区别在于 WHERE 子句过滤表中的单个行,而 HAVING 子句根据条件过滤分组的行。

语法

以下是 MySQL 中 HAVING 子句的基本语法:

SELECT column1, column2, aggregate_function(column)
FROM table_name
GROUP BY column1, column2, ...
HAVING condition
ORDER BY column1, column2, ...;

示例

让我们开始使用以下查询创建一个名为CUSTOMERS的表:

CREATE TABLE CUSTOMERS (
   ID INT NOT NULL,
   NAME VARCHAR (20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),   
   PRIMARY KEY (ID)
);

以下 INSERT 语句将 7 条记录插入到上面创建的表中:

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );

使用以下查询,我们可以验证 CUSTOMERS 表是否已创建:

SELECT * FROM CUSTOMERS;

以下是 CUSTOMERS 表:

ID 姓名 年龄 地址 薪水
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 Hyderabad 4500.00
7 Muffy 24 Indore 10000.00

HAVING 子句与 ORDER BY 子句结合使用

在 MySQL 中,HAVING 子句过滤组,ORDER BY 子句排序结果。当我们将两者结合使用时,HAVING 首先执行,然后根据 ORDER BY 条件对结果集进行排序。

示例

在以下查询中,我们从 CUSTOMERS 表中检索所有薪水总和少于 4540 的记录,并按姓名升序排序:

SELECT NAME, SUM(SALARY) as total_salary
FROM CUSTOMERS
GROUP BY NAME
HAVING SUM(SALARY) < 4540
ORDER BY NAME;

输出

上面查询的输出如下所示:

姓名 总薪水
Kaushik 2000.00
Khilan 1500.00
Komal 4500.00
Ramesh 2000.00

HAVING 子句与 COUNT() 函数结合使用

我们可以将 MySQL HAVING 子句与COUNT()函数结合使用,根据它们包含的行数来过滤组。

示例

在此查询中,我们正在获取年龄计数大于或等于 2 的记录。

SELECT AGE
FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;

输出

CUSTOMERS 表中有两条记录的年龄为 25,因此输出为 25:

年龄
25

HAVING 子句与 AVG() 函数结合使用

MySQL HAVING 子句也可以与AVG()函数结合使用,根据指定列的平均值来过滤组。

示例

在以下查询中,我们尝试返回薪水大于 3000 的客户姓名:

SELECT NAME, AVG(salary) as avg_salary
FROM customers
GROUP BY NAME
HAVING AVG(salary) > 3000;

输出

上面查询的输出如下所示:

姓名 平均薪水
Chaitali 6500.000000
Hardik 8500.000000
Komal 4500.000000
Muffy 10000.000000

HAVING 子句与 MAX() 函数结合使用

在 MySQL 中,我们还可以将 HAVING 子句与MAX()函数结合使用,根据指定列的最大值来过滤组。

示例

在此查询中,我们正在检索最大薪水低于 4000 的客户姓名:

SELECT NAME, MAX(salary) as max_salary
FROM customers
GROUP BY NAME
HAVING MAX(salary) < 4000;

输出

执行给定查询后,输出将显示如下:

姓名 最大薪水
Ramesh 2000.00
Khilan 1500.00
Kaushik 2000.00

使用客户端程序的 HAVING 子句

除了使用 MySQL HAVING 子句根据条件过滤表中的分组行外,我们还可以使用 Node.js、PHP、Java 和 Python 等客户端程序来实现相同的结果。

语法

以下是各种编程语言中此操作的语法:

要通过 PHP 程序过滤表中基于条件的分组行,我们需要使用mysqli函数query()执行带有 HAVING 子句的 SELECT 语句,如下所示:

$sql = "SELECT EXPRESSION1, EXPRESSION2, ...EXPRESSION_N, 
AGGREGATE_FUNCTION(EXPRESSION) FROM TABLE_NAME 
[WHERE CONDITION] GROUP BY EXPRESSION1, 
EXPRESSION2.. EXPRESSION_N HAVING CONDITION";
$mysqli->query($sql);

要通过 Node.js 程序过滤表中基于条件的分组行,我们需要使用mysql2库的query()函数执行带有 HAVING 子句的 SELECT 语句,如下所示:

sql= " SELECT column1, column2, aggregate_function(column) 
FROM table_name GROUP BY column1, column2, ... 
HAVING condition ORDER BY column1, column2, ...";   
con.query(sql);

要通过 Java 程序过滤表中基于条件的分组行,我们需要使用JDBC函数executeUpdate()执行带有 HAVING 子句的 SELECT 语句,如下所示:

String sql = "SELECT column1, column2, aggregate_function(column) 
FROM table_name GROUP BY column1, column2, ... 
HAVING condition ORDER BY column1, column2, ...";  
statement.executeQuery(sql);

要通过 Python 程序过滤表中基于条件的分组行,我们需要使用 MySQLConnector/Pythonexecute()函数执行带有 HAVING 子句的 SELECT 语句,如下所示:

having_clause_query = "SELECT column1, column2, aggregate_function(column) 
FROM table_name GROUP BY column1, column2 HAVING condition"
cursorObj.execute(having_clause_query);

示例

以下是程序:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'TUTORIALS';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($mysqli->connect_errno ) {
   printf("Connect failed: %s
", $mysqli->connect_error); exit(); } //printf('Connected successfully.
'); $sql = 'SELECT tutorial_title, count(tutorial_id) AS tot_count FROM tutorials_tbl WHERE tutorial_id > 1 GROUP BY tutorial_title HAVING count(tutorial_id) > 1'; $result = $mysqli->query($sql); if ($result->num_rows > 0) { printf("Table records: \n"); while($row = $result->fetch_assoc()) { printf("Title: %s, Count: %d", $row["tutorial_title"], $row["tot_count"]); printf("\n"); } } else { printf('No record found.
'); } mysqli_free_result($result); $mysqli->close();

输出

获得的输出如下:

Table records:
Title: Learn MySQL, Count: 2        
var mysql = require('mysql2');
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "Nr5a0204@123"
});

  //Connecting to MySQL
  con.connect(function (err) {
  if (err) throw err;
  console.log("Connected!");
  console.log("--------------------------");

  //Creating a Database
  sql = "create database TUTORIALS"
  con.query(sql);

  //Select database
  sql = "USE TUTORIALS"
  con.query(sql);

  //Creating table
  sql = "CREATE TABLE CUSTOMERS(ID INT NOT NULL,NAME VARCHAR (20) NOT NULL,AGE INT NOT NULL,GENDER VARCHAR (25),SALARY DECIMAL (18, 2),PRIMARY KEY (ID));"
  con.query(sql);

  //Inserting Records
  sql = "INSERT INTO CUSTOMERS VALUES(1, 'Ramesh', 25, 'Male', 2000.00),(2, 'Ramesh', 25, 'Male', 1500.00),(3, 'kaushik', 25, 'Female', 2000.00),(4, 'kaushik', 20, 'Male', 6500.00),(5, 'Hardik', 25, 'Male', 8500.00),(6, 'Komal', 20, 'Female', 4500.00),(7, 'Muffy', 25, 'Male', 10000.00);"
  con.query(sql);

  //Using HAVING Clause
  sql = "SELECT NAME, SUM(SALARY) as total_salary FROM CUSTOMERS GROUP BY NAME HAVING SUM(SALARY) < 4540 ORDER BY NAME;"
  con.query(sql, function(err, result){
    if (err) throw err
    console.log(result)
  });
});             

输出

生成的输出如下:

Connected!
--------------------------
[
  { NAME: 'Komal', total_salary: '4500.00' },
  { NAME: 'Ramesh', total_salary: '3500.00' }
]        
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class HavingClause {
  public static void main(String[] args) {
    String url = "jdbc:mysql://127.0.0.1:3306/TUTORIALS";
    String user = "root";
    String password = "password";
    ResultSet rs;
    try {
      Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user, password);
            Statement st = con.createStatement();
            //System.out.println("Database connected successfully...!");
            String sql = "SELECT NAME, SUM(SALARY) as total_salary FROM CUSTOMERS GROUP BY NAME HAVING SUM(SALARY) < 4540 ORDER BY NAME;";
            rs = st.executeQuery(sql);
            System.out.println("Table records: ");
            while(rs.next()){
              String name = rs.getString("Name");
              String total_salary = rs.getString("total_salary");
              System.out.println("Name: " + name + ", Total_Salary: " + total_salary);
            }
    }catch(Exception e) {
      e.printStackTrace();
    }
  }
}                          

输出

获得的输出如下所示:

Table records: 
Name: Komal, Total_Salary: 4500.00
Name: Ramesh, Total_Salary: 3500.00        
import mysql.connector
#establishing the connection
connection = mysql.connector.connect(
    host='localhost',
    user='root',
    password='password',
    database='tut'
)
# Creating a cursor object 
cursorObj = connection.cursor()
having_clause_query = """SELECT ADDRESS, SUM(SALARY) as total_salary FROM CUSTOMERS
GROUP BY ADDRESS HAVING SUM(SALARY) < 4540 ORDER BY ADDRESS"""
cursorObj.execute(having_clause_query)
filtered_rows = cursorObj.fetchall()
for row in filtered_rows:
    print(row)
cursorObj.close()
connection.close()                                

输出

以下是上述代码的输出:

('Mumbai', Decimal('1200.00'))
广告