oracle数据库中如何用sql语句查出重复字段以及如何删除?
比如我有一个表名字叫student表有一个字段namename字段有100万条数据其中有很多重复数据我如何用sql语句查出重复数据以及如何用sql语句删除重复了的数据(假...
比如我有一个表名字叫student
表有一个字段name
name字段有100万条数据 其中有很多重复数据 我如何用sql语句查出重复数据以及如何用sql语句删除重复了的数据(假设有n个name都一样 我只保留一个) 展开
表有一个字段name
name字段有100万条数据 其中有很多重复数据 我如何用sql语句查出重复数据以及如何用sql语句删除重复了的数据(假设有n个name都一样 我只保留一个) 展开
4个回答
展开全部
查询可用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;
展开全部
查询重复数据
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)
希望能够帮助到你.
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)
希望能够帮助到你.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
假设有一个主键(唯一键)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
)
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
)
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
delete from student where rowID not in(select Max(rowID) from student group by sname)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询