日期:2014-05-18  浏览次数:20408 次

请教一sql语句写法
表A:
字段1       字段2       字段3       字段4
  a               b                 c             1
  a               e                 f             2
  d               b                 c             3
  d               e                 f             4


如表A中,字段1可重复,1、2两条数据中,字段1数据相同,目的是只要其中一条数据,请问该如何写sql语句得出以下结果:

表A:
字段1       字段2       字段3       字段4
  a               b                 c             1
  d               b                 c             3


------解决方案--------------------
create table test(字段1 varchar(10),字段2 varchar(10),字段3 varchar(10),字段4 int)
insert test select 'a ', 'b ', 'c ',1
union all select 'a ', 'e ', 'f ',2
union all select 'd ', 'b ', 'c ',3
union all select 'd ', 'e ', 'f ',4

select * from test a where 字段4=
(
select top 1 字段4 from test where 字段1=a.字段1
)