SQL 删除重复记录,并保留其中一条

SQL:删除重复数据,只保留一条用SQL语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 delete from people where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1) and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1)

3、查找表中多余的重复记录(多个字段) select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录 delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录 select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

6.消除一个字段的左边的第一位:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like ‘村%‘

7.消除一个字段的右边的第一位:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like ‘%村‘

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录 update vitae set ispass=-1 where peopleId in (select peopleId from vitae group by peopleId,seq having count(*) > 1) and seq in (select seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

时间: 2024-10-08 09:38:01

SQL 删除重复记录,并保留其中一条的相关文章

【转】SQL删除重复记录,只保留其中一条

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

如何删除表中的重复记录只保留其中一条?

1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select  peopleId  from  people  group  by  peopleId  having  count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 delete from people  where 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

MySQL删除重复记录只保留一条

删除表中重复记录,只保留一条: delete from 表名 where 字段ID in (select * from (select max(字段ID) from 表名 group by 重复的字段 having count(重复的字段) > 1) as b); 实例:2.当想要为某一个表建立一个唯一索引,由于表中有重复记录而无法进行时,需要删除重复记录.例表 dept id_no id_name 100 'AAA' 101 'BBB' 102 'CCC' 103 'DDD' 100 'EEE

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)来判断,只留有

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

Mysql删除重复记录,保留id最小的一条

mysql 查询重复字段,及删除重复记录的方法MySQL, 数据库, 数据库, 字段, 服务器数据库中有个大表,需要查找其中的名字有重复的记录id,以便比较.如果仅仅是查找数据库中name不重复的字段,很容易:SELECT min(`id`),`name` FROM `table` GROUP BY `name`; 但是这样并不能得到说有重复字段的id值.(只得到了最小的一个id值)查询哪些字段是重复的也容易:SELECT `name`,count(`name`) as count FROM `

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

sql删除重复记录

用SQL语句,删除掉重复项只保留一条 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 所有的合计函数如下表所示: MIN返回一个给定列中最小的数值MAX返回一个给定列中最大的数值SUM返回一个给定列中所有数值的总和AVG返回一个给定列中所有数值的平均值COUNT返回一个给定列中所有数值的个数COUNT(*)返回一个表中的行数假设我们将从employee表中搜索工资最高的列,可以使用以下的SQL语句: SELECT max(salary), dept FR