程序员人生 网站导航

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

栏目:Sqlserver时间:2014-04-08 05:57:23

  网(LieHuo.Net)教程 如何SQL SERVER 查询数据库指定字段重复项的一条数据,今天小编在博客园看到该问题的实例命令代码,现在特别转载了分享给各位访客,希望对您有所帮助。

以下为引用的内容:
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)

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐