我有一个遗留的MySQL表,lnk_lists_addresses
用列list_id
和address_id
.我想编写一个查询,报告所有相同的情况list_id
- address_id
组合在表中出现多次计数.
我试过这个......
SELECT count(*), list_id, address_id FROM lnk_lists_addresses GROUP BY list_id, address_id ORDER BY count(*) DESC LIMIT 20
它的工作原理,因为重复次数少于20次.但是,如何只返回大于1的计数呢?
我尝试WHERE count(*) > 1
在之前和之后添加" " GROUP BY
但是错误地说声明无效.
SELECT count(*), list_id, address_id FROM lnk_lists_addresses GROUP BY list_id, address_id HAVING count(*)>1 ORDER BY count(*) DESC
结合我和Todd.Run的答案,获得更"完整"的答案.您想要使用HAVING子句:
http://dev.mysql.com/doc/refman/5.1/en/select.html