MYSQL 当有两条重复数据时 保留一条

delete from test  where id in (select id from (select  max(id) as id,count(text) as count from test group by text having count >1 order by count desc) as tab )

测试代码

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘22222‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘22222‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘22222‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘22222‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘22222‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘1111‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘22222‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘22222‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘22222‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘22222‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘22222‘,‘33333‘);

INSERT IGNORE INTO test_1(text,text2) values (‘22222‘,‘33333‘);

REPLACE  INTO test(text) values (‘1111‘)

delete from test

select * from test_1 where text=‘1111‘

select * from test_1 where text=‘22222‘

while

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = ‘22222‘ group by text,text2 having count >1 order by count desc) as tab );

来自为知笔记(Wiz)

时间: 2024-10-03 22:39:26

MYSQL 当有两条重复数据时 保留一条的相关文章

MySQL中删除重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT * FROM people WHERE peopleId IN ( SELECT peopleId FROM people GROUP BY peopleId HAVINGcount(peopleId) > 1 ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3

oracle 删除掉重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有

SQL删除重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有r

Oralce中SQL删除重复数据只保留一条(转)

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有

160802、1.06s删除10w条重复记录只保留一条(mysql)及linux删除乱码文件

上次发表过的一遍删除重复记录的文章,其中最优的方案三删除1w条也花了0.07秒而2w条就已经花掉了4秒.今天进行了进一步优化,测试删除10w条只花了1.06秒.速度提升了很多. 建表语句 CREATE TABLE `test_user` ( `id` INT(10) NOT NULL DEFAULT '0', `namea` VARCHAR(40) NOT NULL, `nameb` VARCHAR(40) NOT NULL, `user_id` VARCHAR(40) NOT NULL, PR

MySQL删除重复数据只保留一条

面试碰到一个MySQl的有趣的题目,如何从student表中删除重复名字的行,并保留最小id的记录? 很遗憾当时没有做出来,回家搜索了一番,发现利用子查询的可以很快解决. 1.删除表中多余的重复记录,重复记录是username判断,只留有id最小的记录 delete from studentwhere username in ( select username from studentgroup by username having count(username)>1) and id not i

MySQL:统计两表重复数据出现次数

统计重复数据出现次数 SELECT * FROM ( SELECT * FROM ( SELECT count(t1.compare_id) AS c1,t1.*  FROM t1  LEFT JOIN t3 ON t1.compare_id=t3.compare_id  WHERE t1.compare_id=t3.compare_id  GROUP BY t1.compare_id ) t ) a1 LEFT JOIN ( SELECT * FROM ( SELECT count(t2.co

Mysql 中删除重复数据(保留一条)

sql去重 先根据需要去重的字段进行分组,取到主键值最小的记录(id 是主键,删除重复的 record_id 的记录) select min(id) from tb_traffic_wf_record t group by t.record_id 然后删除不在上面的记录里的数据,就行了 delete from tb_traffic_wf_record where id not in (select min(id) from tb_traffic_wf_record as t group by t

SQL删除重复数据只保留一条数据

1.表结构与数据: CREATE TABLE tablezzl( id int, name VARCHAR(255) ); 2.查询出重复的数据: SELECT name FROM tablezzl GROUP BY name HAVING COUNT(name)>1 3.查询出要保留的重复数据: SELECT MIN(id) ids FROM tablezzl GROUP BY NAME HAVING COUNT(name)>1 4.最终的SQL: DELETE FROM tablezzl