如题,SQL查询和删除重复值,例子是在Oracle环境下,相似写法网上不少。
一、利用distinct关键字去重
二、利用group by分组去重(这里没有实验出来就不写了)
三、利用rowid查询去重(我的推荐这个,rowid查询速度是最快的)测试
先一张测试表(USERS),里面有不少重复数据spa
方法一:distinct关键字code
/*对username字段去重*/ select distinct username from users;
/*根据字段去重,可是多个字段时候只能去全字段重复的数据*/ select distinct username,password from users;
方法二:用rowid方法进行全字段重复查询,也能够按字段查询重复值
注:先查询出最后一条的全字段重复值,在用rowid找出其余剩余的重复值图片
select * from users u01 where rowid!= ( select max(rowid) from users u02 where u01.username=u02.username and u01.password=u02.password and u01.age=u02.age and u01.sex=u02.sex )
删除重复数据(这里删除的是全字段重复的数据,根据不一样状况where后面条件适当修改)it
delete from users u01 where rowid!= ( select max(rowid) from users u02 where u01.username=u02.username and u01.password=u02.password and u01.age=u02.age and u01.sex=u02.sex )