SQL语句删除重复的记录
具体问题是:对于用语句CREATETABLETB(COLANUMBER,COLBNUMBER);创建的表中,里面有一些重复的行,写一个SQL语句删除表中所有重复的记录。请...
具体问题是:对于用语句 CREATE TABLE TB (COLA NUMBER, COLB NUMBER);创建的表中,里面有一些重复的行,写一个SQL语句删除表中所有重复的记录。 请高手给编写一个,多谢
展开
3个回答
2018-07-06 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
删除重复的数据
delete from tb where id not in (
select id from
(select fileSize,fileName ,max(id) id from tb group by filesize,filename ) a
)
现在完成了重复数据的删除,主要是利用了找出某个分组中最大的那个id,其中包括了所有不重复的id,然后使用not in将需要保留的排除。
delete from tb where id not in (
select id from
(select fileSize,fileName ,max(id) id from tb group by filesize,filename ) a
)
现在完成了重复数据的删除,主要是利用了找出某个分组中最大的那个id,其中包括了所有不重复的id,然后使用not in将需要保留的排除。
展开全部
COL1 中有重复记录(COL1,COL2为主键),如何删除
1、有少数重复记录(在col1,col2上有索引比较好)
DELETE T
WHERE (COL1,COL2) IN
(SELECT COL1,COL2 FROM T GROUP BY COL1,COL2 HAVING COUNT(*) > 1)
AND
ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2 HAVING COUNT(*) > 1)
2、大部份记录有重复记录
DELETE T WHERE ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2)
3、其他写法
DELETE T WHERE ROWID IN
(SELECT A.ROWID FROM T A,T B
WHERE A.COL1=B.COL1 AND A.COL2 = B.COL2 AND A.ROWID > B.ROWID)
######################################
10. 删除重复记录
最高效的删除重复记录方法 ( 因为使用了ROWID)
DELETE FROM EMP E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM EMP X
WHERE X.EMP_NO = E.EMP_NO);
11. 用TRUNCATE替代DELETE
当删除表中的记录时,在通常情况下, 回滚段(rollback segments ) 用来存放可以被恢复的信息. 如果你没有COMMIT事务,ORACLE会将数据恢复到删除之前的状态(准确地说是
恢复到执行删除命令之前的状况)
而当运用TRUNCATE时, 回滚段不再存放任何可被恢复的信息.当命令运行后,数据不能被恢复.因此很少的资源被调用,执行时间也会很短.
(译者按: TRUNCATE只在删除全表适用,TRUNCATE是DDL不是DML)
12. 尽量多使用COMMIT
只要有可能,在程序中尽量多使用COMMIT, 这样程序的性能得到提高,需求也会因为COMMIT所释放的资源而减少:
COMMIT所释放的资源:
a. 回滚段上用于恢复数据的信息.
b. 被程序语句获得的锁
c. redo log buffer 中的空间
d. ORACLE为管理上述3种资源中的内部花费
1、有少数重复记录(在col1,col2上有索引比较好)
DELETE T
WHERE (COL1,COL2) IN
(SELECT COL1,COL2 FROM T GROUP BY COL1,COL2 HAVING COUNT(*) > 1)
AND
ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2 HAVING COUNT(*) > 1)
2、大部份记录有重复记录
DELETE T WHERE ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2)
3、其他写法
DELETE T WHERE ROWID IN
(SELECT A.ROWID FROM T A,T B
WHERE A.COL1=B.COL1 AND A.COL2 = B.COL2 AND A.ROWID > B.ROWID)
######################################
10. 删除重复记录
最高效的删除重复记录方法 ( 因为使用了ROWID)
DELETE FROM EMP E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM EMP X
WHERE X.EMP_NO = E.EMP_NO);
11. 用TRUNCATE替代DELETE
当删除表中的记录时,在通常情况下, 回滚段(rollback segments ) 用来存放可以被恢复的信息. 如果你没有COMMIT事务,ORACLE会将数据恢复到删除之前的状态(准确地说是
恢复到执行删除命令之前的状况)
而当运用TRUNCATE时, 回滚段不再存放任何可被恢复的信息.当命令运行后,数据不能被恢复.因此很少的资源被调用,执行时间也会很短.
(译者按: TRUNCATE只在删除全表适用,TRUNCATE是DDL不是DML)
12. 尽量多使用COMMIT
只要有可能,在程序中尽量多使用COMMIT, 这样程序的性能得到提高,需求也会因为COMMIT所释放的资源而减少:
COMMIT所释放的资源:
a. 回滚段上用于恢复数据的信息.
b. 被程序语句获得的锁
c. redo log buffer 中的空间
d. ORACLE为管理上述3种资源中的内部花费
参考资料: http://publish.it168.com/2005/1124/20051124122301.shtml
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询