萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> SQL HAVING 實例教程

SQL HAVING 實例教程

SQL HAVING 實例教程

HAVING
在聽取HAVING添加到SQL ,因為在哪裡關鍵字不能使用聚合函數。

有語法的SQL

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
下面我們來看看HAVING  一個例子吧.
_IdOrderDateOrderPriceCustomer12008/11/121000Hansen22008/10/231600Nilsen32008/09/02700Hansen42008/09/03300Hansen52008/08/302000Jensen62008/10/04100Nilsen

現在,我們要尋找是否有任何的客戶有秩序的總額小於2000年。

我們使用下面的SQL語句:

 

SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
 
CustomerSUM(OrderPrice)Nilsen1700

現在,我們要尋找的客戶,如果“漢森”或“詹森”有秩序的總額超過1500 。

我們添加一個WHERE子句普通的SQL語句:

 

SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
 
結果為.
CustomerSUM(OrderPrice)Hansen2000Jensen2000
copyright © 萬盛學電腦網 all rights reserved