SELECT MAX(id) FROM `tbl`
WHERE createdat BETWEEN ADDDATE(DATE_FORMAT(NOW() ,"%Y-%m-%d"), INTERVAL -6 DAY) AND DATE_FORMAT(NOW(),'%Y-%m-%d')
GROUP BY DATE_FORMAT(createdat,'%Y-%m-%d')
)
前提你的表要有自增主键
mysql中如何查询最近24小时、where visittime >= NOW() - interval 1 hour
昨天、
where visittime between CURDATE()-interval 1 day and CURDATE()
最近7天、
where visittime between CURDATE()-interval 7 day
上周、
where week(visittime)= week(CURDATE()-interval 7 day )
and year(visittime)=year(CURDATE()-interval 7 day )
最近30天、
where visittime between CURDATE()-interval 30 day
这可能需要自定义一个过程procedure了
delimiter $$drop procedure if exists wk
create procedure wk()
begin
declare i int
set i = 1
while i <= 7 do
select date_add(date_sub("2018-09-03",interval 7 day),interval i day)
set i = i + 1
end while
end $$
delimiter
call wk()
注意
date_sub()是减少日期的函数
date_add()是增加日期的函数
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)