MySQL查询最后7天的数据

huanyufeng 2016-07-06 11:49:07
本人新手,半路出家需要用到MySQL,查询最后7天的数据(不是最近)
数据表tablet123
数据:
CREATE TABLE `tablet123` (
`id` int(11) NOT NULL,
`date` varchar(20) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`description` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)


请大神们帮忙看下,谢谢!
...全文
327 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2016-07-06
  • 打赏
  • 举报
回复
-- 最后日期-6 的 select * from tablet123 where cast(date as date) >=( select max(cast(date as date))-6 from tablet123 ) -- 最后 7 天有数据的 select * from tablet123 where cast(date as date) in ( select distinct cast(date as date) from tablet123 order by cast(date as date) desc limit 7)
zjcxc 2016-07-06
  • 打赏
  • 举报
回复
最后7天的定义是什么? 最大的日期-6 到最大日期? 还是最后有数据的7个日期?
huanyufeng 2016-07-06
  • 打赏
  • 举报
回复
引用 4 楼 huanyufeng 的回复:
[quote=引用 2 楼 zjcxc 的回复:] -- 最后日期-6 的 select * from tablet123 where cast(date as date) >=( select max(cast(date as date))-6 from tablet123 ) -- 最后 7 天有数据的 select * from tablet123 where cast(date as date) in ( select distinct cast(date as date) from tablet123 order by cast(date as date) desc limit 7)
我的数据有9月份和10月份的 我尝试查询50天的数据,只能查到10月的,9月之前的查不出来,这个怎么处理?[/quote] 已经自己解决 select * from tablet123 where cast(date as date) >=(select date_sub(max(cast(date as date)), interval 60 day) from tablet123)
huanyufeng 2016-07-06
  • 打赏
  • 举报
回复
引用 2 楼 zjcxc 的回复:
-- 最后日期-6 的 select * from tablet123 where cast(date as date) >=( select max(cast(date as date))-6 from tablet123 ) -- 最后 7 天有数据的 select * from tablet123 where cast(date as date) in ( select distinct cast(date as date) from tablet123 order by cast(date as date) desc limit 7)
我的数据有9月份和10月份的 我尝试查询50天的数据,只能查到10月的,9月之前的查不出来,这个怎么处理?
huanyufeng 2016-07-06
  • 打赏
  • 举报
回复
引用 2 楼 zjcxc 的回复:
-- 最后日期-6 的 select * from tablet123 where cast(date as date) >=( select max(cast(date as date))-6 from tablet123 ) -- 最后 7 天有数据的 select * from tablet123 where cast(date as date) in ( select distinct cast(date as date) from tablet123 order by cast(date as date) desc limit 7)
谢谢兄弟!我的问题已经解决

56,678

社区成员

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

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