我想在MySQL数据库中取出重复的记录。这可以用:

SELECT address, count(id) as cnt FROM list
GROUP BY address HAVING cnt > 1

结果是:

100 MAIN ST    2

我想要拖动它,以便它显示复制的每一行。喜欢的东西:

JIM    JONES    100 MAIN ST
JOHN   SMITH    100 MAIN ST

有什么想法可以做到吗?我试图避免做第一个,然后在代码中用第二个查询查找重复。


当前回答

另一个解决方案是使用表别名,如下所示:

SELECT p1.id, p2.id, p1.address
FROM list AS p1, list AS p2
WHERE p1.address = p2.address
AND p1.id != p2.id

在这种情况下,您真正要做的是获取原始的列表表,从中创建两个假装的表——p1和p2,然后在地址列上执行连接(第3行)。第4行确保相同的记录不会在结果集中多次出现(“重复重复”)。

其他回答

最快重复项删除查询过程:

/* create temp table with one primary column id */
INSERT INTO temp(id) SELECT MIN(id) FROM list GROUP BY (isbn) HAVING COUNT(*)>1;
DELETE FROM list WHERE id IN (SELECT id FROM temp);
DELETE FROM temp;

不是很有效,但应该可以工作:

SELECT *
FROM list AS outer
WHERE (SELECT COUNT(*)
        FROM list AS inner
        WHERE inner.address = outer.address) > 1;
    Find duplicate Records:

    Suppose we have table : Student 
    student_id int
    student_name varchar
    Records:
    +------------+---------------------+
    | student_id | student_name        |
    +------------+---------------------+
    |        101 | usman               |
    |        101 | usman               |
    |        101 | usman               |
    |        102 | usmanyaqoob         |
    |        103 | muhammadusmanyaqoob |
    |        103 | muhammadusmanyaqoob |
    +------------+---------------------+

    Now we want to see duplicate records
    Use this query:


   select student_name,student_id ,count(*) c from student group by student_id,student_name having c>1;

+--------------------+------------+---+
| student_name        | student_id | c |
+---------------------+------------+---+
| usman               |        101 | 3 |
| muhammadusmanyaqoob |        103 | 2 |
+---------------------+------------+---+

要快速查看重复的行,可以运行一个简单的查询

在这里,我正在查询表,并列出所有重复的行相同的user_id, market_place和sku:

select user_id, market_place,sku, count(id)as totals from sku_analytics group by user_id, market_place,sku having count(id)>1;

要删除重复的行,必须决定要删除哪一行。例如id较低(通常较旧)或其他日期信息。在我的情况下,我只是想删除较低的id,因为较新的id是最新的信息。

首先仔细检查是否正确的记录将被删除。在这里,我正在选择将被删除的副本中的记录(通过唯一id)。

select a.user_id, a.market_place,a.sku from sku_analytics a inner join sku_analytics b where a.id< b.id and a.user_id= b.user_id and a.market_place= b.market_place and a.sku = b.sku;

然后我运行delete查询来删除dupes:

delete a from sku_analytics a inner join sku_analytics b where a.id< b.id and a.user_id= b.user_id and a.market_place= b.market_place and a.sku = b.sku;

备份,双重检查,验证,验证备份,然后执行。

SELECT id, count(*) as c  
 FROM 'list'
GROUP BY id HAVING c > 1

这将返回id和该id重复的次数,或者什么都没有,在这种情况下,您将不会有重复的id。

通过(例如:address)更改组中的id,它将返回一个地址由第一个找到的id与该地址重复标识的次数。

SELECT id, count(*) as c  
 FROM 'list'
GROUP BY address HAVING c > 1

我希望这能有所帮助。喜欢。)