1.使用case when方法(不建議使用)
-
結果如下
2.使用時間戳轉換後分組
代碼如下 復制代碼
SELECT
COUNT(DISTINCT user_id) user_count,
FROM_UNIXTIME(
create_time,
'%Y-%m-%d %H:00:00'
)
AS hours,
CONCAT(FROM_UNIXTIME(create_time, '%Y-%m-%d
%H:00'),'-',FROM_UNIXTIME(create_time, '%H')+1,":00") AS `date`
FROM
tb_user_online_log
GROUP BY
hours
ORDER BY
create_time
結果如下 www.111cn.net
注意:case when 效率不高,在數據量大時不推薦使用,這裡僅列出解決方案,僅供參
原文來自:http://www.111cn.net/database/mysql/58986.htm
select count(*) from
(SELECT count(*) FROM 表名 WHERE 條件 GROUP BY id ) a ;
SELECT date_format(FROM_UNIXTIME( `time`),'%Y-%m-%d') AS time,count(*) as count FROM `表名` WHERE 1 group by time