求一sql语句怎么写或者存储过程怎么写?

pawin 2007-10-07 08:31:14
表一 company

id name
1 IBM
2 惠普
3 联想
4 东芝
. .
. .
. .


表二 news

nid comid title
1 1 computer
2 1 notbook
3 2 hard
4 2 pc
. . .
. . .
. . .

表一id和表二comid关联。表一的id和表二的nid都是主键,自增。

要求选出 表二news的前100条记录,其中comid相同的,只能读前3条记录

不知道sql语句怎么写或者存储过程怎么写?

我是很菜的鸟,请高手帮忙帮忙
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
you_tube 2007-10-08
  • 打赏
  • 举报
回复
select top 100 * from (select * news, company where news.id = company.comid) t
where nid in
(
select top 3 nid from (select * from news,company where news.id = company.comid) m where news=t.comid order by nid)
)
dawugui 2007-10-07
  • 打赏
  • 举报
回复
--方法一
select * from (select a.* from news a,company b where a.id = b.comid) t
where nid in
(
select top 3 nid from (select a.* from news a,company b where a.id = b.comid) m where comid=t.comid order by nid
)

--方法二
select * from (select a.* from news a,company b where a.id = b.comid) t
where (select count(*) from (select a.* from news a,company b where a.id = b.comid) m where comid = t.comid and nid < t.nid) < 3
中国风 2007-10-07
  • 打赏
  • 举报
回复


select * from news b
where (select count(1) from news where comid=b.comid and ID!>b.ID)!>3

27,579

社区成员

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

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