想象一下,我有一个包含{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子句,如下所示:
select firstname,count(*) from Customers group by firstname having count(*) > 1 order by 1