我有一个包含以下字段的表:
id | domainname | domain_certificate_no | keyvalue
select语句输出的示例可以是:
id | domainname | domain_certificate_no | keyvalue
===============================================================================
'57092', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_1', '55525772666'
'57093', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_2', '22225554186'
'57094', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_3', '22444356259'
'97168', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_1', '55525772666'
'97169', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_2', '22225554186'
'97170', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_3', '22444356259’
字段domain_certificate_no
是唯一的。我使用group by
来查找重复的keyvalue
但我实际需要的不仅仅是keyvalue
的出现次数,而且还需要逐个列出重复值每个记录中的其他字段发生此值。 group by
无法实现的目标。我怎么能这样做。
答案 0 :(得分:1)
没有经过测试,但我认为这样可以解决问题
SELECT * FROM table
WHERE keyvalue IN (
SELECT keyvalue FROM table GROUP BY keyvalue HAVING COUNT(*) > 1
)
答案 1 :(得分:1)
来自其他人答案的替代方案是来自派生查询的INNER JOIN
。
SELECT a.*
FROM myTable a
INNER JOIN
(
SELECT keyValue
FROM myTable
GROUP BY keyValue
HAVING COUNT(keyValue) > 1
) b
ON a.keyValue = b.keyValue
答案 2 :(得分:0)
没有测试代码,但它可以帮助你更多;)
select group_concat(id) as all_id, group_concat(domainname) as all_domains, group_concat(domain_certificate_no) as all_certificats , keyvalue, count(keyvalue) from table x
group by keyvalue
order by count(keyvalue)