MySQL 日期时间组
我想按小时或天对记录进行分组。
表 A 如下所示: 表A有两列:ID int,record_time datetime,
例如两条记录如下:
id record_time
-----------------------
1 2011-01-24 22:14:50
2 2011-01-24 22:14:50
我想按小时分组。我使用命令:
select *
from A
group by Hour(record_time);
但是,它没有按照我想要的方式输出。 它只输出第一条记录。第二条记录不显示。
I want to group records in hour or day.
The table A looks like:
The table A has two columns: ID int, record_time datetime,
For example, two records looks like:
id record_time
-----------------------
1 2011-01-24 22:14:50
2 2011-01-24 22:14:50
I want to group by hour. I use command:
select *
from A
group by Hour(record_time);
However, it does not output as I want.
It only outputs the first record. The second record does not show.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所谓的分组听起来就像它实际上是排序。将
group by
更改为order by
并看看是否能得到您想要的结果。如果您所说的“组”实际上是指“我想将结果集中的行分组在一起,那么这就是您所需要的(称为排序)。What you call grouping sounds like it's actually sorting. Change
group by
toorder by
and see if that gets you what you want. If by "group" you actually mean "I want to group the rows together in the result set, this is what you need (and is called ordering).更新
试试这个...只需 ORDER BY (无分组)
UPDATE
Try this... just ORDER BY (no grouping)
您应该使用
date_format
,就像you should use
date_format
, like