mysql按时间统计数据
Sql:
SELECT @cdate := date_add( @cdate, INTERVAL - 1 DAY ) date FROM
( SELECT @cdate := date_add( CURDATE(), INTERVAL 1 DAY ) FROM t_car_info ) car
WHERE @cdate >= '2021-11-21 00:00:00' AND @cdate <= '2021-12-21 00:00:00' ;
mysql版本8.0.18执行成功,有结果
mysql版本8.0.25执行成功,无结果
方案1,嵌套查询
SELECT
car1.date
FROM
(
SELECT
@cdate := date_add( @cdate, INTERVAL - 1 DAY ) date
FROM
( SELECT @cdate := date_add( CURDATE(), INTERVAL 1 DAY ) FROM t_car_info ) car
) car1
WHERE
car1.date >= '2021-12-01 00:00:00' AND car1.date <= '2021-12-10 00:00:00';
方案2、limit查询
SELECT @cdate := date_add( @cdate, INTERVAL - 1 DAY ) date FROM
(SELECT @cdate := DATE_ADD( '2021-12-31', INTERVAL 1 day) cdate FROM t_car_info ) car
LIMIT 10