SQL HAVING 实例

现在我们想要查找订单总数少于 2000 的客户。

我们使用下面的 SQL 语句:

下面的 SQL 语句查找注册超过 10 个订单的员工:

实例

SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;

现在我们想哟啊查找员工 "Davolio" 或 "Fuller" 是否拥有超过 25 个订单。

我们在 SQL 语句中增加一个普通的 WHERE 子句:

实例

SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
WHERE LastName='Davolio' OR LastName='Fuller'
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 25;