mysql按年月周日分组统计

2,568次阅读

共计 556 个字符,预计需要花费 2 分钟才能阅读完成。

$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;

正文完
 
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 2019-06-08发表,共计556字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。