MySQL to delete duplicate data retention 1

Test Table cctest table structure

CREATE TABLE `cctest` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `date` year(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Insert test data

truncate table cctest;
insert into cctest values (1,'cc',2019),(2,'cc',2019),(3,'cc',2019),(4,'cc',2020);;   

1. The look-up table superfluous duplicate records, duplicate records is judged based on a single field name

select * from cctest where name in (
select name from cctest group by name
having count(1) > 1
)

Remove duplicate records retention latest piece

delete from cctest where name in(
select * from(
select name from cctest group by name having count(1) > 1) t1)
and id not in(
select * from (
select MAX(id) from cctest group by name having count(1) > 1) t2);

2. The lookup table unnecessary duplicate records, duplicate records is determined according to a plurality of fields name, date

select * from cctest where name in (
select name from cctest group by name,date
having count(1) > 1
)

Remove duplicate records retention latest piece

delete from cctest where (name, date) in(
select t1.name,t1.date from (
select name,date from	cctest	group by name,date having count(1) > 1)t1)
AND id NOT IN (
select t2.maxid from(
select MAX(id) as maxid from cctest group by name,date having count(1) > 1)t2)

Guess you like

Origin blog.csdn.net/u014609263/article/details/90378991