如何更新此选项以选择所有重复项?
SELECT address FROM list
GROUP BY address HAVING count(id) > 1
目前,我认为它只是重新发布了重复的地址。我想要所有的副本。
答案 0 :(得分:8)
Select * from list
where address in (
select address from list group by address
having count(*) > 1);
查看我运行的示例查询:
mysql> select * from flights;
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 6 | 1 |
| 2 | 4 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
10 rows in set (0.00 sec)
mysql> select * from flights where source in
(select source from flights group by source having count(*) > 1);
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 5 | 6 |
| 6 | 1 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
8 rows in set (0.00 sec)
答案 1 :(得分:1)
如果我是正确的,那么您正在寻找包含重复项的实际行 - 因此,如果您有三行具有相同的地址,则返回所有三行。
以下是如何操作:
SELECT * FROM list
WHERE address in (
SELECT address FROM list GROUP BY address HAVING count(id) > 1
);
除非您的地址是“文本”字段,或者您的地址表中有多于几千个重复字段,否则这通常应该有效。
答案 2 :(得分:0)
你在找这个吗?
SELECT * FROM list
WHERE id IN (
SELECT id FROM list
GROUP BY address HAVING count(id) > 1
);