查询多个字段值,且要求所查询的结果中某一个字段的值都不相同

BrillianZhang 2007-05-14 06:00:13
假如有一个表A
我想从中查找字段A1,A2,A3,A4
但是要求,字段A1的值不能有相同的记录,不知如何写Select语句。
...全文
461 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wsyplzbfdl 2007-05-17
  • 打赏
  • 举报
回复
结果
/***********
a b c
1 1 1
3 1 2
4 3 2
************/
wsyplzbfdl 2007-05-17
  • 打赏
  • 举报
回复
当然办法是最苯的了
create table #t(a int,b int,c int)
insert into #t select 1,1,1
union all select 1,1,2
union all select 3,1,2
union all select 3,1,2
union all select 4,3,2
union all select 4,4,2
union all select 4,5,2
union all select 4,6,2

declare t_cursor cursor
for select a,b,c from #t order by a
declare @a int,@b int,@c int
declare @test table(a int,b int,c int)
open t_cursor
fetch next from t_cursor into @a,@b,@c
while @@fetch_status=0
begin
if not exists(select 1 from @test where a=@a)
insert into @test select @a,@b,@c
fetch next from t_cursor into @a,@b,@c
end
select * from @test
close t_cursor
deallocate t_cursor


drop table #t
BrillianZhang 2007-05-17
  • 打赏
  • 举报
回复
看来是没有办法实现了
BrillianZhang 2007-05-15
  • 打赏
  • 举报
回复
谢谢楼上的关注,不过还是不行
ojuju10 2007-05-15
  • 打赏
  • 举报
回复
select a.* from a,(select a1,count(1) as num from a group by a1 ) b
where a.a1=b.a1 and b.num=1
BrillianZhang 2007-05-15
  • 打赏
  • 举报
回复
上面的方法都不行啊
budong0000 2007-05-15
  • 打赏
  • 举报
回复
ft, look this, maybe i haven't know his request.

select * from A where A1 in (select distinct(A1) from A)
shuai45 2007-05-15
  • 打赏
  • 举报
回复
select A1,A2,A3,A4 from 表 as T
where id >(select min(id) from 表 where A1=T.A1)
bill024 2007-05-14
  • 打赏
  • 举报
回复
select * from A where A1 not in
(
select A1 from A group by A1 having(count(*))>1
)
awuniao 2007-05-14
  • 打赏
  • 举报
回复
select DISTINCT A1 from A 可以查询A1不同的值
但查不了A2,A3,A4


awuniao 2007-05-14
  • 打赏
  • 举报
回复
select DISTINCT A1,A2,A3,A4 from A
bill024 2007-05-14
  • 打赏
  • 举报
回复
select top 1 * from A where A1 in (select top 1 A1 from A )
union all
select * from A where A1 not in (select top 1 A1 from A)
bill024 2007-05-14
  • 打赏
  • 举报
回复
select top 1 * from A where A1 in (select top 1 A1 from A )
union all
select * from A t where A1 not in (select top 1 A1 from A)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧