oracle数据库中如何用sql语句查出重复字段以及如何删除?

2024-12-19 08:09:22
推荐回答(4个)
回答1:

查询可用group by语句,删除则用delete语句。

1、创建测试表,插入测试数据:

create table test
(id int,
name varchar2(20));
insert into test values (1,'张三');
insert into test values (1,'张三');
insert into test values (2,'李四');
insert into test values (2,'李四');
insert into test values (3,'王五');
insert into test values (3,'王五');
insert into test values (3,'王五');
insert into test values (4,'赵六');
commit;

2、查询重复数据,用语句:

select id,name from test group by id,name having count(*)>1;

结果:

3、删除重复记录用语句:

delete from test where rowid not in (select min(rowid) from test group by id,name);
commit;

回答2:

查询重复数据
select name,count(*) repeatNum from student group by name having repeatNum > 1
删除重复数据
DELETE tb_affiche WHERE name IN (SELECT name FROM tb_affiche GROUP BY name HAVING COUNT(*) > 1)
AND name not in(SELECT distinct name FROM tb_affiche GROUP BY name HAVING COUNT(*) > 1)
希望能够帮助到你.

回答3:

假设有一个主键(唯一键)id

delete from student a
where exists(
select 1 from
(
select min(id) minid,name
from student
group by name
) b where a.id = b.minid and a.name <> b.name
)

回答4:

delete from student where rowID not in(select Max(rowID) from student group by sname)