50分求一查询语句

feng1021 2007-03-15 03:04:43
test表
ID Type name
1 狗 吉娃娃
2 狗 藏獒
3  狗  哈斯其
4  猫  波斯猫
5  猫  白猫
6  猫  黑猫
7  兔  黄兔
8  兔  白兔
9  兔  黑兔
有张类似的表,其中类型列不止这些品种,我现在想显示出每个品种里的两种,请问SQL语句应该怎么写,谢谢
...全文
516 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lilysl 2007-04-09
  • 打赏
  • 举报
回复
ding
free_pop2k 2007-04-09
  • 打赏
  • 举报
回复
就是用了一个子查询呀
vchao13 2007-04-09
  • 打赏
  • 举报
回复
select * from tb t
where name in
(
select top 2 name from tb where type=t.type order by name
)

看不明白为什么这样写,请高解释一下!
谢谢!
free_pop2k 2007-04-07
  • 打赏
  • 举报
回复
抢答得真快呢.牛人一大把
cradeve 2007-04-07
  • 打赏
  • 举报
回复
select * from 表 a where id in (select top 2 id from 表 where type=a.type order by id )
onlyonereason 2007-04-07
  • 打赏
  • 举报
回复
都说了
free_pop2k 2007-04-07
  • 打赏
  • 举报
回复
select * from 表 a where id in (select top 2 id from 表 where type=a.type order by id )
saiwong 2007-03-16
  • 打赏
  • 举报
回复
晕,来晚了,不会没分吧,LZ怎么不结贴
IT_zen 2007-03-16
  • 打赏
  • 举报
回复
强人真多................
ALLEN625314 2007-03-15
  • 打赏
  • 举报
回复
顶....很好用的一招
feng1021 2007-03-15
  • 打赏
  • 举报
回复
谢谢楼上的各位,好用了
dawugui 2007-03-15
  • 打赏
  • 举报
回复
if object_id('pubs..tb') is not null
drop table tb
go

create table tb(ID int,Type varchar(10),name varchar(10))
insert into tb(ID,Type,name) values(1,'狗','吉娃娃')
insert into tb(ID,Type,name) values(2,'狗','藏獒')
insert into tb(ID,Type,name) values(3,'狗','哈斯其')
insert into tb(ID,Type,name) values(4,'猫','波斯猫')
insert into tb(ID,Type,name) values(5,'猫','白猫')
insert into tb(ID,Type,name) values(6,'猫','黑猫')
insert into tb(ID,Type,name) values(7,'兔','黄兔')
insert into tb(ID,Type,name) values(8,'兔','白兔')
insert into tb(ID,Type,name) values(9,'兔','黑兔')

select * from tb t
where name in
(
select top 2 name from tb where type=t.type order by name
)

drop table tb
/*
ID Type name
----------- ---------- ----------
2 狗 藏獒
3 狗 哈斯其
4 猫 波斯猫
5 猫 白猫
8 兔 白兔
9 兔 黑兔

(所影响的行数为 6 行)
*/
paoluo 2007-03-15
  • 打赏
  • 举报
回复

Select * From TEST A
Where (Select Count(ID) From TEST Where Type = A.Type And ID < A.ID) < 2

Or

Select * From TEST A
Where (Select Count(ID) From TEST Where Type = A.Type And ID > A.ID) < 2
zlp321002 2007-03-15
  • 打赏
  • 举报
回复

declare @t table( id int ,type varchar(10), name varchar(10))
insert into @t select 1,'狗','吉娃娃'
union all select 2,'狗','藏獒'
union all select 3,'狗','哈斯其'
union all select 4,'猫','波斯猫'
union all select 5,'猫','白猫'
union all select 6,'猫','黑猫'
union all select 7,'兔','黄兔'
union all select 8,'兔','白兔'
union all select 9,'兔','黑兔'

select * from @t a where id in (select top 2 id from @t where type=a.type order by id )

/*
id type name
----------- ---------- ----------
1 狗 吉娃娃
2 狗 藏獒
4 猫 波斯猫
5 猫 白猫
7 兔 黄兔
8 兔 白兔

(所影响的行数为 6 行)

*/
mengmou 2007-03-15
  • 打赏
  • 举报
回复
来晚了。
WangZWang 2007-03-15
  • 打赏
  • 举报
回复
Select * from 表 as a where name in
(Select top 2 name from 表 where type=a.type)
zlp321002 2007-03-15
  • 打赏
  • 举报
回复
select * from 表 a where id in (select top 2 id from 表 where type=a.type order by id )

27,580

社区成员

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

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