mysql生成一个虚拟连续时间区间作为统计分析使用

SELECT time FROM (
   SELECT @num:=@num+1,
      CONCAT(DATE_FORMAT(
         SUBDATE(
            ADDDATE(#{startTime}, INTERVAL @num WEEK),
            WEEKDAY(ADDDATE(#{startTime}, INTERVAL @num WEEK))
         ),'%y-%m-%d'),
         '~',
         DATE_FORMAT(SUBDATE(
            ADDDATE(#{startTime}, INTERVAL @num WEEK),
            WEEKDAY(ADDDATE(#{startTime}, INTERVAL @num WEEK))-6
         ),'%y-%m-%d')) time
   FROM (SELECT 1 FROM st_work_order LIMIT 30) n,(SELECT @num:=-1) t 
   WHERE ADDDATE(#{startTime}, INTERVAL @num WEEK) < DATE_FORMAT(#{endTime},'%Y-%m-%d')
   ORDER BY time
)x;



(SELECT @num:=@num+1 num, DATE_FORMAT(ADDDATE('2020-08-02', INTERVAL @num DAY),'%Y-%m-%d') date
FROM (SELECT 1 FROM base_user LIMIT 120) n, (SELECT @num:=-1) t 
WHERE ADDDATE('2020-08-02', INTERVAL @num DAY) < DATE_FORMAT('2020-08-08','%Y-%m-%d')
ORDER BY date)

 

你可能感兴趣的:(mysql)