sql查询去掉重复记录
rst.open SQL,CONN,1,3
if not rst.eof then
while not rst.eof
response.Write(rst("ACTION_VALUE"))
rst.movenext
wend
end if
rst.close
%>
查出来的结果全是一样的重复结果,怎么去掉重复的?
我是新手,请教方法,我查了半天了,因为我的语句有点复杂,所以不会写
你把sql语句改下:
select distinct 字段 from 表
如果有重复数据,那么他只显示一条!
你觉得这个语句适合这么改么?你改给我看下? 展开
1、打开要去掉重复数据的数据库,这里新建一张含有重复数据的user表做示例,如下图所示:
2、输入“select * from user where name in (select name from user group by name having count(name) > 1) ”sql语句,点击运行可以看到查询出了数据库中user表的重复数据。
3、通过“delete from user where name in (select name from user group by name having count(name) > 1) ”sql语句删除姓名重复的数据。
4、也可以通过“select distinct name from user”sql语句来去掉重复数据,这里去掉了张三的重复数据。
5、通过“select distinct class from user”sql语句来去掉班级相同的重复数据,如下图所示:
以下为去重方法。三个方法。效率1 >2>3 推荐使用第一条
[sql] view plain copy print?
1,Select * from stuinfo a where not exists(select 1 from stuinfo where stuName=a.stuName and ID<a.ID)
2,select a.* from stuinfo a join (select min(ID)ID,stuName from stuinfo group by stuName) b on a.stuName=b.stuName and a.ID=b.ID
3,select * from stuinfo a where ID=(select min(ID) from stuinfo where stuName=a.stuName)
Select * from stuinfo a where not exists(select 1 from stuinfo where stuName=a.stuName and ID<a.ID)
select a.* from stuinfo a join (select min(ID)ID,stuName from stuinfo group by stuName) b on a.stuName=b.stuName and a.ID=b.ID
select * from stuinfo a where ID=(select min(ID) from stuinfo where stuName=a.stuName)
扩展资料
有重复数据主要有一下几种情况:
1,存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉
example: select distinct * from table(表名) where (条件)
2,存在部分字段相同的纪录(有主键id即唯一键)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
example:
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
3,没有唯一键ID
这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:
example:
select identity(int1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
drop table newtable
参考资料来源:百度百科 - 结构化查询语言
sql查询去掉重复记录可以参考以下操作:
if exists(select * from sysobjects where name='stuInfo')
drop table stuInfo
create table stuInfo /*创建学员信息表**/
(
stuName varchar(20) not null,-- 姓名,非空
stuNo char(6) not null,-- 学号,非空
stuAge int not null,-- 年龄,int 默认为4个长度
stuId numeric(18,0),
stuSeat smallint ,-- 坐位
stuAddress text -- 住址 可以为空
)
-- 给stuInfo添加一列
alter table stuInfo add id int identity(1,1) primary key;
if exists(select * from sysobjects where name='stuInfo')
drop table stuInfo
create table stuInfo /*创建学员信息表**/
(
stuName varchar(20) not null,-- 姓名,非空
stuNo char(6) not null,-- 学号,非空
stuAge int not null,-- 年龄,int 默认为4个长度
stuId numeric(18,0),
stuSeat smallint ,-- 坐位
stuAddress text -- 住址 可以为空
)
-- 给stuInfo添加一列
alter table stuInfo add id int identity(1,1) primary key;
需求:只要数据stuName 相同,则说明是两条重复的记录
以下为去重方法。三个方法。效率1 >2>3 推荐使用第一条
[sql] view plain copy print?
1. Select * from stuinfo a where not exists(select 1 from stuinfo where stuName=a.stuName and ID<a.ID)
2. select a.* from stuinfo a join (select min(ID)ID,stuName from stuinfo group by stuName) b on a.stuName=b.stuName and a.ID=b.ID
3. select * from stuinfo a where ID=(select min(ID) from stuinfo where stuName=a.stuName)
Select * from stuinfo a where not exists(select 1 from stuinfo where stuName=a.stuName and ID<a.ID)
select a.* from stuinfo a join (select min(ID)ID,stuName from stuinfo group by stuName) b on a.stuName=b.stuName and a.ID=b.ID
select * from stuinfo a where ID=(select min(ID) from stuinfo where stuName=a.stuName)
扩展资料:
1、查找全部重复记录
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
2、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
3、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
4、查找表中多余的重复记录(多个字段)
select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
5、删除表中多余的重复记录(多个字段),只留有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)
6、查找表中多余的重复记录,不包含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
SELECT * FROM t_info a WHERE ((SELECT COUNT(*) FROM t_info WHERE Title = a.Title) > 1) ORDER BY Title DESC.
1、查找全部重复记录
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1).
2、过滤重复记录(只显示一条)
Select * From HZT Where ID In (Select Max(ID) From HZT Group By Title).
注:此处显示ID最大一条记录
扩展资料
有两个以上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。
一、对于第一种重复,比较容易解决,使用select distinct * from tableName就可以得到无重复记录的结果集。如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
1、select distinct * into #Tmp from tableName.
2、drop table tableName.
3、select * into tableName from #Tmp.
4、drop table #Tmp.
发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。
二、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下:
假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集 :
1、select identity(int,1,1) as autoID, * into #Tmp from tableName.
2、select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID.
3、select * from #Tmp where autoID in(select autoID from #tmp2).