求个sql语句

jom_ch 2015-10-25 05:00:57

有如下表结构:

picid uid field1 field2 ...

1 9527
2 9528
3 9528
4 9527
...

也就是每个用户对应N张图片,现在需要找出指定的每个用户的前4张图片,求这个sql语句,多谢!
...全文
158 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2015-10-27
  • 打赏
  • 举报
回复
参考下贴中的多种方法 http://blog.csdn.net/acmain_chm/article/details/4126306 [征集]分组取最大N条记录方法征集,及散分....
文修 2015-10-27
  • 打赏
  • 举报
回复
select * from tb A where 4>=(select count(*) from tb B where A.uid=B.uid and A.picid<B.picid)
wwwwb 2015-10-27
  • 打赏
  • 举报
回复
select * from tb A where 4>=(select count(*) from tb B where A.uid=B.uid and A.picid<B.picid)
  • 打赏
  • 举报
回复
SELECT *
FROM 
(
SELECT *,
       (SELECT COUNT(*) FROM TB T2 WHERE T1.UID = T2.UID AND T1.PICID  <= T2.PICID) RN
FROM TB T1
)T
WHERE RN <= 4
rucypli 2015-10-26
  • 打赏
  • 举报
回复
select * from tb A where 4>=(select count(*) from tb B where A.uid=B.uid and A.uid<B.uid)
jom_ch 2015-10-26
  • 打赏
  • 举报
回复
引用 1 楼 zhangbin1988 的回复:
select * from table where uid = '' order by picid limit 4
抱歉,我可能没有说清楚,应该是多个uid ,每个uid的前4张图片 where uid in(9527,9528...)
ayzen1988 2015-10-26
  • 打赏
  • 举报
回复
select * from table where uid = '' order by picid limit 4
jom_ch 2015-10-26
  • 打赏
  • 举报
回复
引用 3 楼 rucypli 的回复:
select * from tb A where 4>=(select count(*) from tb B where A.uid=B.uid and A.uid<B.uid)
大拿 ,结尾部分是不是写错了?
lkw15209712520 2015-10-26
  • 打赏
  • 举报
回复
select * from ( select *,row_number over(partition by uid order by picid asc) as idd from table where uid in (9527,9528...) where idd <=4;

56,675

社区成员

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

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