透過將 MySQL SUM() 函數與 HAVING 子句結合使用,它會根據 HAVING 子句後給出的特定條件過濾結果。要理解上述概念,請考慮一個「employee_tbl」表,該表具有以下記錄 -
mysql> SELECT * FROM employee_tbl; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in set (0.00 sec)
現在,下面的查詢將使用 HAVING 子句和 SUM() 函數來獲取超過 250 的 daily_typing_pages 總數。
mysql> Select Name, SUM(daily_typing_pages) From employee_tbl GROUP BY NAME HAVING SUM(daily_typing_pages) > 250; +------+-------------------------+ | name | SUM(daily_typing_pages) | +------+-------------------------+ | Jack | 270 | | Zara | 650 | +------+-------------------------+ 2 rows in set (0.17 sec)
以上是我們如何將 MySQL SUM() 函數與 HAVING 子句一起使用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!