MySQL having子句

2019-10-16 22:58:10

在本教學中,您將學習如何使用MySQL HAVING子句為行分組或聚合組指定過濾條件。

MySQL HAVING子句簡介

SELECT語句中使用HAVING子句來指定一組行或聚合的過濾條件。

HAVING子句通常與GROUP BY子句一起使用,以根據指定的條件過濾分組。如果省略GROUP BY子句,則HAVING子句的行為與WHERE子句類似。

請注意,HAVING子句將過濾條件應用於每組分行,而WHERE子句將過濾條件應用於每個單獨的行。

MySQL HAVING子句範例

讓我們舉一些使用HAVING子句的例子來看看它是如何工作。 我們將使用範例資料庫(yiibaidb)中的orderdetails表進行演示。

mysql> desc orderdetails;
+-----------------+---------------+------+-----+---------+-------+
| Field           | Type          | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+-------+
| orderNumber     | int(11)       | NO   | PRI | NULL    |       |
| productCode     | varchar(15)   | NO   | PRI | NULL    |       |
| quantityOrdered | int(11)       | NO   |     | NULL    |       |
| priceEach       | decimal(10,2) | NO   |     | NULL    |       |
| orderLineNumber | smallint(6)   | NO   |     | NULL    |       |
+-----------------+---------------+------+-----+---------+-------+
5 rows in set

可以使用GROUP BY子句來獲取訂單號,檢視每個訂單銷售的商品數量和每個銷售總額:

SELECT 
    ordernumber,
    SUM(quantityOrdered) AS itemsCount,
    SUM(priceeach*quantityOrdered) AS total
FROM
    orderdetails
GROUP BY ordernumber;

執行上面查詢語句,得到以下結果(部分) -

+-------------+------------+----------+
| ordernumber | itemsCount | total    |
+-------------+------------+----------+
|       10100 | 151        | 10223.83 |
|       10101 | 142        | 10549.01 |
|       10102 | 80         | 5494.78  |
|       10103 | 541        | 50218.95 |
|       10104 | 443        | 40206.20 |
|       10105 | 545        | 53959.21 |
|       10106 | 675        | 52151.81 |
| ------- 這裡省略了一大波資料 ---------|
| ........                ........... |
|       10421 | 75         | 7639.10  |
|       10422 | 76         | 5849.44  |
|       10423 | 111        | 8597.73  |
|       10424 | 269        | 29310.30 |
|       10425 | 427        | 41623.44 |
+-------------+------------+----------+
326 rows in set

現在,可以通過使用HAVING子句查詢(過濾)哪些訂單的總銷售額大於55000,如下所示:

SELECT 
    ordernumber,
    SUM(quantityOrdered) AS itemsCount,
    SUM(priceeach*quantityOrdered) AS total
FROM
    orderdetails
GROUP BY ordernumber
HAVING total > 55000;

執行上面查詢語句,得到以下結果 -

+-------------+------------+----------+
| ordernumber | itemsCount | total    |
+-------------+------------+----------+
|       10126 | 617        | 57131.92 |
|       10127 | 540        | 58841.35 |
|       10135 | 607        | 55601.84 |
|       10142 | 577        | 56052.56 |
|       10165 | 670        | 67392.85 |
|       10181 | 522        | 55069.55 |
|       10192 | 585        | 55425.77 |
|       10204 | 619        | 58793.53 |
|       10207 | 615        | 59265.14 |
|       10212 | 612        | 59830.55 |
|       10222 | 717        | 56822.65 |
|       10287 | 595        | 61402.00 |
|       10310 | 619        | 61234.67 |
|       10312 | 601        | 55639.66 |
|       10390 | 603        | 55902.50 |
+-------------+------------+----------+

可以使用邏輯運算子(如ORAND)在HAVING子句中構造複雜過濾條件。 假設您想查詢哪些訂單的總銷售額大於50000,並且包含超過600個專案,則可以使用以下查詢:

SELECT 
    ordernumber,
    SUM(quantityOrdered) AS itemsCount,
    SUM(priceeach*quantityOrdered) AS total
FROM
    orderdetails
GROUP BY ordernumber
HAVING total > 50000 AND itemsCount > 600;

執行上面查詢語句,得到以下結果 -

+-------------+------------+----------+
| ordernumber | itemsCount | total    |
+-------------+------------+----------+
|       10106 | 675        | 52151.81 |
|       10126 | 617        | 57131.92 |
|       10135 | 607        | 55601.84 |
|       10165 | 670        | 67392.85 |
|       10168 | 642        | 50743.65 |
|       10204 | 619        | 58793.53 |
|       10207 | 615        | 59265.14 |
|       10212 | 612        | 59830.55 |
|       10222 | 717        | 56822.65 |
|       10310 | 619        | 61234.67 |
|       10312 | 601        | 55639.66 |
|       10360 | 620        | 52166.00 |
|       10390 | 603        | 55902.50 |
|       10414 | 609        | 50806.85 |
+-------------+------------+----------+

假設您想查詢所有已發貨(status='Shiped')的訂單和總銷售額大於55000的訂單,可以使用INNER JOIN子句將orders表與orderdetails表一起使用,並在status列和總金額(total)列上應用條件,如以下查詢所示:

SELECT 
    a.ordernumber, status, SUM(priceeach*quantityOrdered) total
FROM
    orderdetails a
        INNER JOIN
    orders b ON b.ordernumber = a.ordernumber
GROUP BY ordernumber, status
HAVING status = 'Shipped' AND total > 5000;

執行上面查詢,得到以下結果 -

+-------------+---------+----------+
| ordernumber | status  | total    |
+-------------+---------+----------+
|       10126 | Shipped | 57131.92 |
|       10127 | Shipped | 58841.35 |
|       10135 | Shipped | 55601.84 |
|       10142 | Shipped | 56052.56 |
|       10165 | Shipped | 67392.85 |
|       10181 | Shipped | 55069.55 |
|       10192 | Shipped | 55425.77 |
|       10204 | Shipped | 58793.53 |
|       10207 | Shipped | 59265.14 |
|       10212 | Shipped | 59830.55 |
|       10222 | Shipped | 56822.65 |
|       10287 | Shipped | 61402.00 |
|       10310 | Shipped | 61234.67 |
|       10312 | Shipped | 55639.66 |
|       10390 | Shipped | 55902.50 |
+-------------+---------+----------+

HAVING子句僅在使用GROUP BY子句生成高階報告的輸出時才有用。 例如,您可以使用HAVING子句來回答統計問題,例如在本月,本季度或今年總銷售額超過10000的訂單。

在本教學中,您已經學習了如何使用具有GROUP BY子句的MySQL HAVING子句為行分組或聚合分組指定過濾器條件。