MySQL 日期时间组

发布于 2024-10-15 00:17:41 字数 348 浏览 4 评论 0原文

我想按小时或天对记录进行分组。

表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓬勃野心 2024-10-22 00:17:42

你所谓的分组听起来就像它实际上是排序。将 group by 更改为 order by 并看看是否能得到您想要的结果。如果您所说的“组”实际上是指“我想将结果集中的行分组在一起,那么这就是您所需要的(称为排序)。

What you call grouping sounds like it's actually sorting. Change group by to order 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).

[浮城] 2024-10-22 00:17:42
SELECT *
FROM A 
GROUP BY DATE_FORMAT(record_time, '%H')

更新

SELECT *
FROM A 
ORDER BY DATE_FORMAT(record_time, '%H%Y%m%d')

试试这个...只需 ORDER BY (无分组)

SELECT *
FROM A 
GROUP BY DATE_FORMAT(record_time, '%H')

UPDATE

SELECT *
FROM A 
ORDER BY DATE_FORMAT(record_time, '%H%Y%m%d')

Try this... just ORDER BY (no grouping)

眼角的笑意。 2024-10-22 00:17:42

您应该使用 date_format,就像

group by date_format(record_time, '%Y%m%d%H');

you should use date_format, like

group by date_format(record_time, '%Y%m%d%H');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文