沃梦达 / IT编程 / 数据库 / 正文

MySQL实现按天统计最近七天数据

mysql实现按天统计最近7天的数据示例,希望能够帮到大家! $time = time(); $today = strtotime(date('Y-m-d', time()));//零点时间戳 $todayDate = date('Y-m-d', time()); //最近7天 $sevenDaysAgo = strtotime(date('Y-m-d', time())) - 7 * 86400; $sql = "SELECT t1.day, COUNT(t2.id) user_numFROM (SEL

mysql实现按天统计最近7天的数据示例,希望能够帮到大家!
$time = time();
        $today = strtotime(date('Y-m-d', time()));//零点时间戳
        $todayDate = date('Y-m-d', time());
        //最近7天
        $sevenDaysAgo = strtotime(date('Y-m-d', time())) - 7 * 86400;
 $sql = "SELECT
  t1.day,
  COUNT(t2.id) user_num
FROM
  (SELECT
    @cdate := DATE_ADD(@cdate, INTERVAL - 1 DAY) day
  FROM
    (SELECT
      @cdate := DATE_ADD('{$todayDate}', INTERVAL + 1 DAY)
    FROM
      area) t0
  LIMIT 7) t1
  LEFT JOIN
    (SELECT
      FROM_UNIXTIME(create_time,'%Y-%m-%d') day,
      id
    FROM
      user_log
    WHERE create_time > $sevenDaysAgo
      AND create_time < $time
      ) t2
    ON t2.day = t1.day
GROUP BY t1.`day`";
 

本文标题为:MySQL实现按天统计最近七天数据