--刪除重復列
a.如果有ID字段,就是具有唯一性的字段delect table where id not in (select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用到判斷重复的字段b.,如果是判斷所有字段
select * into #aa from table group by id1,id2,....
delete table table
insert into table
select * from #aac.如果表中有ID的情況select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
col1+','+col2+','...col5 組合主鍵
select * from table where col1+','+col2+','...col5 in (select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用到判斷重复的字段d.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)e.
alter table yourtable add rownum int identity(1,1)
go
delete from yourtable where rownum not in (select min(rownum ) from yourtable group by 你重复的字段名)
go
alter table yourtable drop column rownum
gof.
alter table 表 add newfield int identity(1,1)
delete 表
where newfield not in(
select min(newfield) from 表 group by 除newfield外的所有字段
)alter table 表 drop column newfield
g.
-- 刪除表中重復的記錄
DELETE delete1
FROM tabTest delete1
JOIN tabTest delete2
ON delete1.student_id=delete2.student_id AND delete1.course_id=delete2.course_id AND delete1.id>delete2.id
a.如果有ID字段,就是具有唯一性的字段delect table where id not in (select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用到判斷重复的字段b.,如果是判斷所有字段
select * into #aa from table group by id1,id2,....
delete table table
insert into table
select * from #aac.如果表中有ID的情況select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
col1+','+col2+','...col5 組合主鍵
select * from table where col1+','+col2+','...col5 in (select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用到判斷重复的字段d.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)e.
alter table yourtable add rownum int identity(1,1)
go
delete from yourtable where rownum not in (select min(rownum ) from yourtable group by 你重复的字段名)
go
alter table yourtable drop column rownum
gof.
alter table 表 add newfield int identity(1,1)
delete 表
where newfield not in(
select min(newfield) from 表 group by 除newfield外的所有字段
)alter table 表 drop column newfield
g.
-- 刪除表中重復的記錄
DELETE delete1
FROM tabTest delete1
JOIN tabTest delete2
ON delete1.student_id=delete2.student_id AND delete1.course_id=delete2.course_id AND delete1.id>delete2.id
如果是为了显示全部内容,不要用group by 就是了
drop procedure [dbo].[p_qry]
GO/*--查询重复记录的通用存储过程
可以查询出表中那些数据是重复的,这里的重复,是指除主键外重复的记录
如果表中有主键,请指定主键.
如果表中有标识字段,而且标识字段无重复,请在调用时,将主键指定为标识字段
如果标识字段重复,不能用此存储过程-- 2004.4--*/create proc p_qry
@tbname sysname, --要查询的表名
@keyfdname sysname=null --表中的主键,如果未指定,则表中无主键
as
declare @nokey bit,@fd varchar(8000),@tj varchar(8000)
set nocount on
if isnull(@keyfdname,'')=''
begin
select @keyfdname=cast(newid() as char(36)),@nokey=1
exec('alter table ['+@tbname+'] add ['+@keyfdname+'] decimal(38,0) identity(1,1)')
end
select @fd='',@tj=''
select @fd=@fd+',['+name+']'
,@tj=@tj+'['+name+']=a.['+name+'] and '
from syscolumns
where object_name(id)=@tbname and name<>@keyfdname
set @fd=substring(@fd,2,8000)
exec('select '+@fd+' from ['+@tbname+'] a
where exists(select 1 from ['+@tbname
+'] where '+@tj+'['+@keyfdname+']<>a.['+@keyfdname+'])')
if @nokey=1
exec('alter table ['+@tbname+'] drop column ['+@keyfdname+']')
set nocount off
go--调用示例
--创建测试数据
create table 表(f1 int,f2 int,f3 int,f4 int,f5 int)
insert into 表
select 1,1,1,1,1
union all select 2,1,1,1,1
union all select 3,2,1,23,1
union all select 4,2,3,1,3
union all select 5,1,1,1,1
go--调用通用存储过程实现楼主的查询
exec p_qry '表','f1'--删除测试环境
drop table 表
/*--测试结果f2 f3 f4 f5
----------- ----------- ----------- -----------
1 1 1 1
1 1 1 1
1 1 1 1
--*/
where having count(*)>1
group by col1,col2,col3,col4 怎么会是先having后 group by
不明白
a.如果有ID字段,就是具有唯一性的字段delect table where id not in (select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用到判斷重复的字段b.,如果是判斷所有字段
select * into #aa from table group by id1,id2,....
delete table table
insert into table
select * from #aac.如果表中有ID的情況select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
col1+','+col2+','...col5 組合主鍵
select * from table where col1+','+col2+','...col5 in (select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用到判斷重复的字段d.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)e.
alter table yourtable add rownum int identity(1,1)
go
delete from yourtable where rownum not in (select min(rownum ) from yourtable group by 你重复的字段名)
go
alter table yourtable drop column rownum
gof.
alter table 表 add newfield int identity(1,1)
delete 表
where newfield not in(
select min(newfield) from 表 group by 除newfield外的所有字段
)alter table 表 drop column newfield
g.
-- 刪除表中重復的記錄
DELETE delete1
FROM tabTest delete1
JOIN tabTest delete2
ON delete1.student_id=delete2.student_id AND delete1.course_id=delete2.course_id AND delete1.id>delete2.id
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)
会提示having有错
我在数据库中选出的记录有100,用distinct选出的记录只有80条,则我要知道除这80条语句外的20,怎么写呀?
insert t1(a,b)
select 'aa','bb'
union all select 'a1','bgb'
union all select 'aa','bb'
union all select 'a2','bb'
union all select 'aa3','beeb'
union all select 'aa','bb'
union all select 'a2','bb'delete t1
where id not in
(select min(id) as id from t1 group by a,b)----------------a,b重复.-----------select * from t1drop table t1/*
id a b
------------------
0 aa bb
1 a1 bgb
3 a2 bb
4 aa3 beeb*/
from t t1,t t2
where t1.唯一字段=t2.唯一字段 and t1.编号>t2.编号
然后
select * from test e,test f
where e.a=f.a and e.b<>f.b
则可以显示那些重复的记录