如何从一个表中选出不同类型的各两个?

zairwolfd 2008-07-30 01:36:19
表a字段为: id,name,type

要求一条语句选出每种type前两条记录
...全文
102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvjx125 2008-08-06
  • 打赏
  • 举报
回复
select * from test where test.id in (select top N id from test as t where test.class=t.class order by t.class,t.date asc) order by test.class,test.date
楼主你用的是什么数据库,如果你用的不是MYSQL的话这个可以
要上MYSQL的话,我就不知道了
wjazz 2008-08-02
  • 打赏
  • 举报
回复
select * from test group by type union all select * from test where id not in(select id from test group by type) group by type order by id desc

经过测试

暂时想出来这个,如果有更优化的可以通知我下

PS:楼上给的不太好,如果TYPE有很多种,那就麻烦了
jhwcd 2008-08-02
  • 打赏
  • 举报
回复
sql="select top 2 * from a where [type]=1 union all select top 2 * from a where [type]=2 order by id desc"

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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