简体   繁体   English

从mysql中的表中删除连续的重复项

[英]Delete consecutive duplicates from a table in mysql

I have a database , where table contains consecutive duplicate rows . 我有一个数据库,其中的表包含连续的重复行。 Demo of table with data is as follows. 带数据表的演示如下。

id  name  processed
 1  xyz      0
 2  xyz      0
 3  ABC      0
 4  ABC      0

I want to delete the consecutive duplicate from this table , and once duplicate is deleted update processed to 1. So that the final table looks like follows. 我想从该表中删除连续的重复项,一旦删除重复项,更新处理为1。因此最终表如下所示。

id  name  processed
 1  xyz      1
 3  ABC      1

I am doing it as follow. 我正在做如下。

SET @v1 := (select group_concat(`id`) from `names` as m1 where 0 < (select count(*) from `names` as m2 where m2.`id` = m1.`id` - 1 and m2.`name` = m1.`name`));
DELETE FROM names WHERE id IN (@v1);
UPDATE names SET `processed`=1 WHERE `processed`=0

The query works fine , but it deletes one row at a time . 该查询工作正常,但一次删除了一行。 Please help me on this.I want all the selected rows to be deleted . 请对此提供帮助。我希望所有选定的行都被删除。 Thanks in advance. 提前致谢。

As @MarkBaker already wrote in comment, you can try DELETE FROM table WHERE name=name and id>id . 正如@MarkBaker在评论中所写,您可以尝试DELETE FROM table WHERE name=name and id>id

But that's only fix to what already has been done. 但这仅解决了已经完成的事情。 To prevent that, you should add unique index to name column. 为了防止这种情况,您应该在name列中添加unique索引。 That should prevent any duplicates of being added in future. 那应该防止将来添加任何重复项。

You can't set unique index when you have duplicates though, so you need clean first :) 但是,如果有重复项,则无法设置unique索引,因此需要先清理:)

You cannot UPDATE and DELETE in the same query. 您不能在同一查询中进行UPDATE和DELETE。 So that pretty much leaves you with this: 因此,您几乎可以做到这一点:

DROP TABLE IF EXISTS my_table;
CREATE  TABLE my_table 
(id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,name  CHAR(3) NOT NULL,processed TINYINT NOT NULL DEFAULT 0);

INSERT INTO my_table VALUES
(1  ,'xyz',      0),
(2  ,'xyz',      0),
(3  ,'ABC',      0),
(4  ,'ABC',      0);

SELECT * FROM my_Table;
+----+------+-----------+
| id | name | processed |
+----+------+-----------+
|  1 | xyz  |         0 |
|  2 | xyz  |         0 |
|  3 | ABC  |         0 |
|  4 | ABC  |         0 |
+----+------+-----------+

SELECT y.* FROM my_table x JOIN my_table y ON y.id = x.id + 1 AND y.name = x.name;
+----+------+-----------+
| id | name | processed |
+----+------+-----------+
|  2 | xyz  |         0 |
|  4 | ABC  |         0 |
+----+------+-----------+

DELETE y FROM my_table x JOIN my_table y ON y.id = x.id + 1 AND y.name = x.name;
Query OK, 2 rows affected (0.00 sec)


UPDATE my_table SET processed = 1;
Query OK, 2 rows affected (0.00 sec)

SELECT * FROM my_table;
+----+------+-----------+
| id | name | processed |
+----+------+-----------+
|  1 | xyz  |         1 |
|  3 | ABC  |         1 |
+----+------+-----------+

For PHP and MySQL , If your all data is consecutive pairs then this will work. 对于PHPMySQL ,如果您的所有数据都是连续对,那么它将起作用。

$con = mysqli_connect('host', 'user', 'pass', 'db');
$query ="select m1.id from names as m1 where 0 < (select count(*) from names as m2 where m2.id = m1.id - 1 and m2.name = m1.name)";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)){
  $query2 ="DELETE FROM names WHERE id = ".$row['id'];
  mysqli_query($con, $query2);
  $id = $row['id']-1;
  $query3 ="UPDATE names SET `processed`=1 WHERE id = ".$id;
  mysqli_query($con, $query3);
}

I checked it and its working fine. 我检查了它,并且工作正常。 Hope it works for you too. 希望它也对您有用。

This will not edit your table, but will give you SELECT with desired result: 这不会编辑您的表,但是会为您提供具有期望结果的SELECT:

SELECT min(id) id, name, 1 processed
FROM mytable
GROUP BY name

You can use this in CREATE TABLE newtable AS SELECT ... , and then DROP mytable , and finally to ALTER TABLE newtable RENAME TO mytable . 您可以在CREATE TABLE newtable AS SELECT ... ,然后在DROP mytable ,最后在DROP mytable中将ALTER TABLE newtable RENAME TO mytable

The DISTINCT keyword can be used to return only distinct (different) values. DISTINCT关键字只能用于返回不同的(不同的)值。 Use this query: 使用此查询:

SELECT DISTINCT `id`, `name`,`1` AS processed
FROM mytable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM