分组且要用limit,sql该怎么写

phoebezhy 2014-03-27 01:52:43
有一个历史表,包括时间record_time和内容record_content,现在要取前5天,且每天取前5条数据,一个sql该怎么写?
...全文
269 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
benluobo 2014-03-27
  • 打赏
  • 举报
回复
不会,那可能是你的应用程序存在问题
phoebezhy 2014-03-27
  • 打赏
  • 举报
回复
引用 1 楼 benluobobo 的回复:
select record_time, record_content from( select *, if(@ct = substring(tmp1.record_time,1,10), @rank:=@rank+1, @rank:= 1) rank, @ct:=substring(tmp1.record_time,1,10) from (select @rank=0, record_time,record_content from tableName order by record_time desc) tmp1) tmp2 where rank <=5
这个sql可以,但是很奇怪的就是放到php代码里跑就不对了,单独在数据库里跑是对的。。。和临时变量有关吗?
ACMAIN_CHM 2014-03-27
  • 打赏
  • 举报
回复
select * from 一个历史表 a where record_time>curdate()-interval 5 days where 5>(select count(*) from 一个历史表 where date date(record_time)=date(a.record_time) and record_time>a.record_time)
WWWWA 2014-03-27
  • 打赏
  • 举报
回复
表中有无唯一标识的字段?
benluobo 2014-03-27
  • 打赏
  • 举报
回复
select record_time, record_content from( select *, if(@ct = substring(tmp1.record_time,1,10), @rank:=@rank+1, @rank:= 1) rank, @ct:=substring(tmp1.record_time,1,10) from (select @rank=0, record_time,record_content from tableName order by record_time desc) tmp1) tmp2 where rank <=5

56,687

社区成员

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

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