mysql delete duplicate rows in table

1 First create a table for demonstration. Create the table first as shown in the figure
insert image description here

2 Then switch to the command line to view the table content in the database, there are indeed duplicate items.

insert image description here

3 First query out the duplicate rows
insert image description here

4 Now what you want to delete is the data that appears in this table, and only keep the row with the largest id in each group, so you need to use the max function to query the largest row, so first find out that these ids are not in the largest row or query the
insert image description hereid The maximum value in these
insert image description here
and then will not be in these non-repeated ones, these can be deleted

delete from deleteDuplicate where id not in (select t.maxId from (select max(id) as maxId from deleteDuplicate group by name,age,country) t);

insert image description here

Guess you like

Origin blog.csdn.net/m0_56184347/article/details/124505557