sql删除重复数据
一:創建表
create table userinfo
(
id int,
name varchar(10),
age int,
sex int,
address varcahr(50)
)
二:添加數據
insert into userinfo values(1,'admin','20,'1','上海市');
insert into userinfo values(2,'admin','20,'1','上海市');
insert into userinfo values(3,'admin','20,'1','上海市');
insert into userinfo values(4,'admin','20,'1','上海市');
insert into userinfo values(4,'admin','20,'1','上海市');
commit;
三:查詢重復的數據
select id, name,age,sex,address from userinfo where (name,age,sex,address) in (select name,age,sex,address from userinfo group by name,age,sex,address having count(1) > 1);
四:保留最小id的一行數據
select id, name,age,sex,address from userinfo where (name,age,sex,address) in (select name,age,sex,address from userinfo group by name,age,sex,address having count(1) > 1) and id not in(select min(id) from userinfo group by name,age,sex,address having count(1) >1);
五:刪除重復數據
delete from usrinfo where id in (select id from userinfo where (name,age,sex,address) in (select name,age,sex,address from userinfo group by name,age,sex,address having count(1) > 1) and id not in(select min(id) from userinfo group by name,age,sex,address having count(1) >1));
commit;
注: 如果沒有id,可以用oracle中的ROWNUM來添加虛擬的id;
轉載于:https://www.cnblogs.com/andy-do/archive/2013/04/12/3015799.html
總結
- 上一篇: sizeof运算求结构体大小
- 下一篇: GC DevKit 快速入门 -- 游戏