排除字段重复行的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)
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)
相关文章推荐
- SQL排除重复结果只取字段最大值分享
- SQL排除重复结果只取字段最大值
- SQL排除重复结果只取字段最大值分享
- sql删除几个字段值相同的重复列
- sql 删除表中多余的重复记录(多个字段),只保留一条记录
- sql中排除字段列为空字符串的列
- 一句简单的SQL语句,找出表中某个字段的重复值
- SQL中统计某字段不重复个数
- SQL 删除重复的数据(多个字段判断),只保留一条数据
- sql语句删除数据表重复字段的方法
- sql查询列、字段重复值及操作
- SQL语句 删除表user 中字段name 内容重复的记录,
- sql 删除表中某字段的重复数据
- 查询同一表内多字段同时重复记录的SQL语句
- mysql 用sql 语句去掉某个字段重复值数据的方法
- SQL语句查询重复字段并按数量排序
- sql 字段重复值,in,like
- 关于SQL中查询重复字段只显示一条记录
- sql查询出一张表里重复的字段
- 查询同一表内多字段同时重复记录的SQL语句