WHERE 子句過濾行,應用於分組前;HAVING 子句過濾組,應用於分組後且可使用組聚合函數的結果。
WHERE 與HAVING 子句的差異
在MySQL 中,WHERE 和HAVING 子句都用於過濾資料集,但它們在作用範圍和使用方式上存在主要差異。
WHERE 子句
HAVING 子句
使用範例
WHERE 子句:
<code class="sql">SELECT * FROM customers WHERE age > 30;</code>
此查詢從customers 表中選擇所有年齡大於30 的客戶。
HAVING 子句:
<code class="sql">SELECT city, COUNT(*) AS customer_count FROM customers GROUP BY city HAVING customer_count > 100;</code>
此查詢從 customers 資料表中選擇所有客戶數量超過 100 個的城市。
總結
以上是mysql中where和having的區別的詳細內容。更多資訊請關注PHP中文網其他相關文章!