mysql - How to get the current rounded hour in MySQL?
问题描述
select current_time()
gives:
16:43:16
how can I modify it to get:
16:00
I tried to play with it a bit and I wrote this:
select TIMESTAMPDIFF(HOUR,CURDATE(),current_timestamp())
Which gives:
16
I can convert it to string and add :00
but I think there might be easier way to get what I want.
Any ideas?
解决方案
就个人而言,我会使用DATE_FORMAT
适当的格式,包括文字,例如
SELECT DATE_FORMAT( CURRENT_TIME() ,'%H:00')
MySQL 也提供了等价的TIME_FORMAT
功能
SELECT TIME_FORMAT( CURRENT_TIME() ,'%H:00')
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-format
推荐阅读
- c - 为什么我的递归函数即使到达返回(0)也不会停止?
- python - 在 python 中通过循环从一个文本文件到另一个文本文件进行搜索,同时每个搜索项仅返回一个结果
- corda - 在 Windows 上运行 Cordite 网络映射 JAR 时无法运行程序 npm 错误
- c# - 在http-context响应C#中分块下载文件
- mongodb - 从 getMongo 处理器中提取日期时的 $numberlong 格式
- javascript - setTimeout 如何在 JavaScript 中实现 promise?
- angularjs - AngularJS 延迟加载路由无法正常工作
- android - 验证 RecyclerView 中的重复条目
- php - 当我上传带有变体的更新产品的 excel 时,shopify Api 中出现错误(api 客户端每秒超过 2 次调用)
- git - 无法在 AWS CodeCommit 中合并拉取请求。抱怨有冲突,但是远程和本地都是最新的