想象一下,我有一个客户数据库表,其中包含{id,username,firstname,lastname}
如果我想查找有多少个具有不同名字的实例,可以执行以下操作:
select firstname,count(*) from Customers group by 2 order by 1; username | count(*) =================== bob | 1 jeff | 2 adam | 5
如何编写相同的查询以仅返回多次出现的名字?即在上面的示例中,仅返回jeff和adam的行。
您需要该having子句,如下所示:
having
select firstname, count(*) from Customers group by firstname having count(*) > 1 order by 1