同类数据只取一行怎么写?

[复制链接]
查看11 | 回复9 | 2008-9-27 09:35:45 | 显示全部楼层 |阅读模式
SELECT a.id,a.bid FROM a,b WHERE a.bid = b.id
返回:
idfid
12
22
32
42
53
63
73
83
我想返回这样:
idfid
1 2
5 3
就是具有相同fid值的记录只返回一个
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
select min(id),fid
from a
group by fid
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
上面那句怎么改啊?
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
最初由 xiaodong_1567 发布
[B]select min(id),fid
from a
group by fid [/B]




回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
select * from table a where not exists(select 1 from table b where a.fid=b.fid and a.id>b.id)
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
呵呵,路過。。。。
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
表:a
id name
11 aaaa
11 bbbb
11 cccc
22 dddd
22 eeee
22 ffff

如何将表中的相同id号的第一条记录取出来?即:
id name
11 aaaa
22 dddd
select id1=identity(int,1,1),* into #t from a
go
select id,name from #t where id1 in(select min(id1) from #t group by id)
select * from a b
where name=(select top 1 name from a where id=b.id)
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
请问7楼的答案具体是什么?哪个是搜寻的啊?哪个是正确的啊
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
具体答案上面有.
有两段SQL语句,两个都能达到楼主的要求.
回复

使用道具 举报

千问 | 2008-9-27 09:35:45 | 显示全部楼层
select id1=identity(int,1,1),* into #t from a
go
select id,name from #t where id1 in(select min(id1) from #t group by id)
这个我不大明白,我的QQ是120783975
有什么SQL的QQ群吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行