About sql delete duplicate data

mysql delete duplicate data

Remove duplicate data

Requirements: Query the database, delete duplicate data, and retain one piece of data.
code show as below:

Idea one):

Use a multi-table connection to use one table as two tables. Because the ids are different, delete the data with duplicate content but different ids in the where judgment condition, and keep one. Delete the extra table at the end.
(Optimal solution):

delete b from biao  a join biao  b on a.classes=b.classes where a.id>b.id;

delete 表名 from 表名 as 别名 join 表名 as 别名 on 链接条件 where 判断条件

Idea two):

(The latest version seems to be unavailable)
First group all the data (so that the non-repetitive data can be made into a table), and then use a new query to check it again (because the query table in sql cannot be directly modified or Delete), in the where judgment, only need to compare the original table with the grouped table, as long as the id of the repeated data is not equal to the grouped id, delete it

delete from biao where id!= all(select a.id from (select id from biao group by classes) a);

delete from 表名 where id(主键) != all(select id from (select id from 表名 group by 需绑定的所有字段名)别名)

Guess you like

Origin blog.csdn.net/JL_Java/article/details/109232527