gpt4 book ai didi

MySQL:从日期时间字段获取 "busiest"或 "most popular"小时?

转载 作者:可可西里 更新时间:2023-11-01 07:12:13 25 4
gpt4 key购买 nike

考虑下表,其中包含字段 - id (int) 和 date_created (datetime):

id       date_created

1 2010-02-25 12:25:32
2 2010-02-26 13:40:37
3 2010-03-01 12:02:22
4 2010-03-01 12:10:23
5 2010-03-02 10:10:09
6 2010-03-03 12:45:03

我想知道这组数据一天中最忙/最流行的时间。在此示例中,我要查找的结果是 12。

想法?

最佳答案

要仅获取最流行的时段,请使用此查询

select date_format( date_created, '%H' ) as `hour`
from [Table]
group by date_format( date_created, '%H' )
order by count(*) desc
limit 1;

如果你想查看所有数据,就用这个

select count(*) as num_records
, date_created
, date_format( date_created, '%H' ) as `hour`
from [Table]
group by `hour`
order by num_records desc;

关于MySQL:从日期时间字段获取 "busiest"或 "most popular"小时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2471546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com