mysql按年月周日分组统计

  • mysql按年月周日分组统计已关闭评论
  • 302 views
  • A+
所属分类:MySql
$groupBy = '%Y%m%d'; //按日
if ($cycle == 'week') $groupBy = '%Y%u'; //按周
if ($cycle == 'month') $groupBy = '%Y%m'; //按月

$gameWhere = $gameId > 0 ? ' game_id = ' . $gameId . ' AND ': '';

$sql = <<<EOF
SELECT DATE_FORMAT(created_time, '$groupBy') AS `day`, COUNT(DISTINCT(uid)) AS person FROM `t1` 
WHERE $gameWhere created_time >= :startTime AND created_time <= :endTime GROUP BY DATE_FORMAT(created_time, '$groupBy')
UNION
SELECT DATE_FORMAT(created_time, '$groupBy') AS `day`, COUNT(DISTINCT(uid)) AS person FROM `t2` 
WHERE $gameWhere created_time >= :startTime2 AND created_time <= :endTime2 GROUP BY DATE_FORMAT(created_time, '$groupBy')
EOF;
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin