请教三表inner join,取其中一个表的前2个记录的问题

unm 2011-05-01 03:37:41
SELECT a.content, a.postdate, c.username
FROM (
表1 a
INNER JOIN 表2 b ON a.uid = b.uid
)
INNER JOIN 表3 c ON a.uid = c.uid
ORDER BY username, postdate DESC
LIMIT 50

表1表2表三通过用户的uid关联

如果想取得每一个用户的前2条表1记录,怎么取得啊
...全文
118 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2011-05-01
  • 打赏
  • 举报
回复
SELECT a.content, a.postdate, c.username
FROM 表1 a INNER JOIN 表2 b ON a.uid = b.uid
INNER JOIN 表3 c ON a.uid = c.uid
where 2>(select count(*) from 表1 where uid=a.uid and postdate>a.postdate)
ORDER BY username, postdate DESC
LIMIT 50
rucypli 2011-05-01
  • 打赏
  • 举报
回复
select *
from tb A
where (select count(*) from tb b where A.username=b.username and A.id>b.id)<2
ACMAIN_CHM 2011-05-01
  • 打赏
  • 举报
回复
参考下贴中的多种方法

http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
[征集]分组取最大N条记录方法征集,及散分....
rucypli 2011-05-01
  • 打赏
  • 举报
回复
select *
from tb A
where (select count(*) from tb where A.username=b.username and A.id>b.id)>2

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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