英文:
Always round set seconds / minutes /hours to 00 mysql
问题
在我的数据库中,我有以下日期时间记录:
2019-12-12 01:56:46.825
2019-12-12 02:56:47.831
2019-12-12 02:56:47.831
我想要能够检索这些记录,其中小时、分钟或秒设置为00值:
例如,我想要返回秒和毫秒设置为00的记录:
2019-12-12 01:56:00.000
2019-12-12 02:56:00.000
2019-12-12 02:56:00.000
或者将它们的分钟也设置为零。
2019-12-12 01:00:00.000
2019-12-12 02:00:00.000
2019-12-12 02:00:00.000
我该如何编写查询以按所需格式返回我的结果?
我尝试过:
SELECT DISTINCT
concat(
date_format(data_log.time,'%Y-%m-%d '),
TIME_FORMAT(SEC_TO_TIME(((TIME_TO_SEC(data_log.time) +30) DIV 60) * 60), '%H:%i:%s'))
AS result
这将返回秒设置为00的结果,所以我离目标更近了。
英文:
In my database I have the following date-times:
2019-12-12 01:56:46.825
2019-12-12 02:56:47.831
2019-12-12 02:56:47.831
I want to be able to retrieve these records with the hours, minutes or seconds set to their 00 values:
For example I want to be able to return records with seconds and milliseconds set to 00:
2019-12-12 01:56:00.000
2019-12-12 02:56:00.000
2019-12-12 02:56:00.000
Or return them setting the minutes to zero as well.
2019-12-12 01:00:00.000
2019-12-12 02:00:00.000
2019-12-12 02:00:00.000
How can I write a query to format my results as desired?
I tried :
SELECT DISTINCT
concat(
date_format(data_log.time,'%Y-%m-%d '),
TIME_FORMAT(SEC_TO_TIME(((TIME_TO_SEC(data_log.time) +30) DIV 60) * 60), '%H:%i:%s'))
AS result
This returns the result with seconds set to 00 so im getting closer
答案1
得分: 3
如果你知道你想将时间的哪些部分设置为零,那么一个简单的
SELECT DATE_FORMAT('2009-10-04 22:23:22.023', '%Y-%m-%d %H:00:00.000');
将返回
2009-10-23 22:00:00.000
或者
SELECT DATE_FORMAT('2009-10-04 22:23:22.023', '%Y-%m-%d %H:%i:00.000');
将返回
2009-10-23 22:23:00.000
英文:
If you know which parts of the time you want to get as zero then a simple
SELECT DATE_FORMAT('2009-10-04 22:23:22.023', '%Y-%m-%d %H:00:00.000');
will return
2009-10-23 22:00:00.000
Or
SELECT DATE_FORMAT('2009-10-04 22:23:22.023', '%Y-%m-%d %H:%i:00.000');
will return
2009-10-23 22:23:00.000
答案2
得分: 0
str_to_date()
函数在这种情况下运行正常:
str_to_date(datecol, '%Y-%m-%d %H:%i')
或
str_to_date(datecol, '%Y-%m-%d %H')
查看演示。
英文:
The function str_to_date()
works fine in this case:
str_to_date(datecol, '%Y-%m-%d %H:%i')
or
str_to_date(datecol, '%Y-%m-%d %H')
See the demo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论