注册 登录
编程论坛 SQL Server论坛

[求助]关于查询问题

重在参与 发布于 2007-03-15 13:47, 526 次点击
id type name
1 狗 京吧
2 狗 臧敖
3 狗 哈斯其
4 狗 吉娃娃
5 猫 波斯猫
6 猫 波小猫
7 猫 波大猫
8 猫 波中猫

这张表我想提出狗的前两名和猫的前两名,应该怎么做,请指教,谢谢.
6 回复
#2
重在参与2007-03-15 14:17

没人理?

#3
棉花糖ONE2007-03-15 16:24

if object_id('t') is not null
drop table t
go
create table t(id int identity,type nvarchar(1),name nvarchar(3))
insert into t select N'狗',N'京吧'
union all select N'狗',N'臧敖'
union all select N'狗',N'哈斯其'
union all select N'狗',N'吉娃娃'
union all select N'猫',N'波斯猫'
union all select N'猫',N'波小猫'
union all select N'猫',N'波大猫'
union all select N'猫',N'波中猫'

select * from t

select * from t t1 where id in (select top 2 id from t where type=t1.type)

id type name
----------- ---- ----
1 狗 京吧
2 狗 臧敖
5 猫 波斯猫
6 猫 波小猫

(所影响的行数为 4 行)

#4
cyyu_ryh2007-03-15 17:35

棉花糖
你那个t1是代表什么哦

#5
cyyu_ryh2007-03-15 17:38
还有它的排名有可能不是由小到大排列的,或者是混乱的。这样的话
#6
bygg2007-03-15 22:20
t1 是原表.
#7
冰封情愁2007-03-16 18:32

不管排序怎么乱法,三楼的方法都可以!!

1