查询语句请教

zzh20 2003-10-09 02:27:17
类似:select X from table where 分类 in('分类1','分类2','分类3') group by 分类

要求一次取出表里每个分类的最新3条记录,3种分类的话就是一起取出3*3条记录,该如何写语句呀? 谢谢回复!
...全文
31 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzh20 2003-10-09
  • 打赏
  • 举报
回复
U
zzh20 2003-10-09
  • 打赏
  • 举报
回复
d
zzh20 2003-10-09
  • 打赏
  • 举报
回复
to txlicenhe(马可):
我用您的:
select * from table a where X in ('分类1','分类2','分类3') and X in
(select top 3 X from table where X=a.X order by X desc)
order by X

提示:当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。

请问用exists该如何写呀,我试了一下,还是有问题,急请解决,谢谢!
txlicenhe 2003-10-09
  • 打赏
  • 举报
回复
参考:
http://expert.csdn.net/Expert/topic/2222/2222072.xml?temp=4.813784E-02

select * from table a where X in ('分类1','分类2','分类3') and X in
(select top 3 X from table where X=a.X order by X desc)
order by X
yelook 2003-10-09
  • 打赏
  • 举报
回复

select top 3 * into #temp from table where 分类 = '分类1' order by id desc
insert into #temp select top 3 * from table where 分类 = '分类2' order by id desc
insert into #temp select top 3 * from table where 分类 = '分类3' order by id desc
select * from #temp
drop table #temp
伍子V5 2003-10-09
  • 打赏
  • 举报
回复
select top 3 * from table where 分类='分类1'
union all
select top 3 * from table where 分类='分类2'
union all
select top 3 * from table where 分类='分类3'


愉快的登山者 2003-10-09
  • 打赏
  • 举报
回复
select *, identity(int,1,1) id into #t from [table] where 分类 in ('分类1','分类2','分类3')
select * from #t as A where id in (select top 3 id from #t where 分类 = A.分类 order by id desc)
愉快的登山者 2003-10-09
  • 打赏
  • 举报
回复
select *, identity(int,1,1) id into #t from [table] where 分类 in ('分类1','分类2','分类3')
select * from #t as A where id in (select top 3 id from #t where 分类 = A.分类)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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