MySQL GROUP BY和HAVING子句

时间:2016-05-13 16:42:07

标签: mysql

我正在尝试编写一个脚本,以显示每个城市至少有两个客户的名称,以及该城市的客户数量。

这是我所拥有的,但我无法弄清楚如何获得客户数量。 我接近了吗?

SELECT CONCAT (FName,' ',LName) AS Customers, city 
FROM Customer
GROUP BY City
HAVING COUNT(CID) >= 2

2 个答案:

答案 0 :(得分:0)

使用count(*)

 SELECT city , count(*)
 FROM Customer 
 GROUP BY City HAVING COUNT(*) >= 2

答案 1 :(得分:0)

正如你从这个例子中看到的那样:

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;

您必须在选择

中输入COUNT(CID)