Mysql delete duplicate records

When testing after writing a function, multiple duplicate records are often inserted into the database, and it will be troublesome to manually delete one by one, so how to delete duplicate records?
The following unfolds from two aspects:

  • id is different, other fields are all repeated
  • No id field, all fields are repeated

id is different, other fields are all repeated

The data in the table is as follows:
id is different, other fields are the same

delete from test 
where id not in (
	select tmp.mins from 
	(
		select 
			min(id) as mins 
		from test
		group by name, address
	)tmp 
)

By grouping repeated fields, take min(id), and delete all other records in the test table whose id is not in this id set, so as to achieve the effect of deduplication (you can also take max(id))

No id field, all fields are repeated

The data in the table is as follows:
Fields are all repeated

There is no hidden rowid attribute for each row of mysql records, so it is difficult to delete. You can only manually add the row number first.

SELECT @i:=@i+1 AS id, name, tile FROM test,(SELECT @i:=0) rowNo;

Subsequent processing is the same as the previous section

Guess you like

Origin blog.csdn.net/huhui806/article/details/88112502