HAVING子句用于在GROUP BY子句之后對分組進(jìn)行篩選。它允許你篩選基于聚合函數(shù)計算結(jié)果的分組。
例如,假設(shè)有一張訂單表,包含訂單號和訂單金額。要篩選出訂單金額大于1000的訂單,可以使用如下SQL語句:
SELECT order_number, SUM(amount) as total_amount
FROM orders
GROUP BY order_number
HAVING total_amount > 1000;
這將返回所有訂單金額大于1000的訂單號及其總金額。HAVING子句可以幫助你篩選出符合特定條件的分組數(shù)據(jù)。