求一SQL语句,谢谢!

dzmiao 2005-02-15 02:54:12
t1 --主题

id--cid--title
1---1----a
2---2----b
3---1----c
4---2----d
5---3----e
6---2----f
7---3----g
8---3----h
9---1----i

t2 --分类

cid--cate
1----kk
2----mm
3----dd

=========如何取每个分类的最新两条记录,即:


3---1----c
4---2----d

6---2----f
7---3----g
8---3----h
9---1----i


谢谢!
...全文
153 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ReViSion 2005-02-15
  • 打赏
  • 举报
回复
select * from t1 a,
(select top 2 id from t1 where cid=a.id order by id desc) b
where a.id=b.id
alipaymate 2005-02-15
  • 打赏
  • 举报
回复
学习一下
didoleo 2005-02-15
  • 打赏
  • 举报
回复
select * from t1 a where (select count(1) from t1 where cid=a.cid and id>=a.id) between 1 and 2
chinaandys 2005-02-15
  • 打赏
  • 举报
回复
select * from t1 a
where id in (select top 2 id from t1 where cid=a.id order by id desc)
zjcxc 2005-02-15
  • 打赏
  • 举报
回复
select * from t1 a,t2 b
where a.cid=b.cid
and a.id in(
select top 2 id from t1 where cid=b.cid order by id desc)
txlicenhe 2005-02-15
  • 打赏
  • 举报
回复
好久没写,手生了,试一下:

select * from t1 a
where id in (select top 2 id from t1 where cid = a.cid)
zhang_yzy 2005-02-15
  • 打赏
  • 举报
回复
select
a.id,a.cid,a.title
from
t1 a
where
(select
count(1)
from
t1 b
where
b.cid =a.cid
and b.id >a.id ) <1
dzmiao 2005-02-15
  • 打赏
  • 举报
回复
好久没写,手生了,试一下:

select * from t1 a
where id in (select top 2 id from t1 where cid = a.cid)

---------------------------以上返回结果不对

select * from t1 a,t2 b
where a.cid=b.cid
and a.id in(
select top 2 id from t1 where cid=b.cid order by id desc)


select * from t1 a where (select count(1) from t1 where cid=a.cid and id>=a.id) between 1 and 2

---------------------------以上正确

select * from t1 a
where id in (select top 2 id from t1 where cid=a.id order by id desc)


--------------------------以上无记录返回

select * from t1 a,
(select top 2 id from t1 where cid=a.id order by id desc) b
where a.id=b.id

--------------------------服务器: 消息 107,级别 16,状态 2,行 1
列前缀 'a' 与查询中所用的表名或别名不匹配。


感谢各位!
da21 2005-02-15
  • 打赏
  • 举报
回复
楼上的select * from t1 a,
中的
t1 a
是什么意思????????

27,579

社区成员

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

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