SQL HAVING 关键字

HAVING

HAVING 在聚合函数中使用条件命令而不是 WHERE

下面的 SQL 列出了每个国家/地区的客户数量。仅包括拥有 5 个以上客户的国家/地区:

实例
  1. SELECT COUNT(CustomerID), Country
  2. FROM Customers
  3. GROUP BY Country
  4. HAVING COUNT(CustomerID) > 5;

以下 SQL 列出了每个国家/地区的客户数量,按从高到低的顺序排列(仅包括客户超过 5 个的国家/地区):

实例
  1. SELECT COUNT(CustomerID), Country
  2. FROM Customers
  3. GROUP BY Country
  4. HAVING COUNT(CustomerID) > 5
  5. ORDER BY COUNT(CustomerID) DESC;

分类导航