您的位置:首页 > 数据库

排除字段重复行的SQL

2007-11-30 11:13 225 查看
--
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[test]
GO

CREATE TABLE [dbo].[test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[score] [int] NULL
) ON [PRIMARY]
GO
truncate table test
Insert into test(name,score)
select 'a',11 union
select 'b',22 union
select 'b',33 union
select 'c',44 union
select 'c',55
--

SELECT * FROM test

select * from test where [id] in (select [id] from
(
select name,id=(select min([id]) from test where [name]=test1.name) from
(select distinct [name] from test)test1
)test2
)

或者

select * from test where [id] in(select min([id]) from test group by name)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: