查询数据库指定字段重复项的一条数据

CREATE TABLE [cc] (
 [a] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [b] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [c] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

--数据:
"1","a","c"
"2","a","c"
"3","b","c"
"4","c","d"
"5","c","d"

 

CREATE TABLE [temp] (
 [id] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

 

--实现
declare @cname nvarchar(20)
declare @mname nvarchar(20)
Declare Cur Cursor For select distinct b,c from cc
Open Cur
Fetch next From Cur Into @cname,@mname
While @@fetch_status=0    
Begin
 insert into temp
 select top 1 a from cc where b=@cname and c=@mname
 Fetch next From Cur Into @cname,@mname
End  
Close Cur  
Deallocate Cur


select * from cc

select * from cc where a in (select id from temp)

posted @ 2009-09-11 10:26  fighter  阅读(482)  评论(0编辑  收藏  举报