轻松上手,快乐学习!

SQL HAVING 关键字


HAVING

使用HAVING命令代替WHERE和聚合函数。

以下SQL列出了每个Country的客户数量。仅包括拥有超过5个客户的Country:

实例

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

运行实例»

以下SQL列出了每个Country的客户数量,从高到低排序(仅包括客户数超过5的Country):

实例

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

运行实例»