Mrli
别装作很努力,
因为结局不会陪你演戏。
Contacts:
QQ博客园

开发过程中Mysql API记录

2021/12/26 Mysql 开发记录
Word count: 243 | Reading time: 1min

时间操作

TIME_TO_SEC

MySQL TIME_TO_SEC(time) 函数返回将参数 time 转换为秒数的时间值,转换公式为“小时 ×3600+ 分钟 ×60+ 秒”。

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> SELECT TIME_TO_SEC('15:15:15');
+-------------------------+
| TIME_TO_SEC('15:15:15') |
+-------------------------+
| 54915 |
+-------------------------+
1 row in set (0.00 sec)
mysql> select TIME_TO_SEC("2021-02-02 04:04:04"); -- 只会考虑时分秒,不会考虑年月日
+-------------------------+
| TIME_TO_SEC("2021-02-02 04:04:04") |
+-------------------------+
| 14644 |
+-------------------------+

具体实例:

1
2
SELECT SEC_TO_TIME(TIME_TO_SEC(时间字段)- TIME_TO_SEC(时间字段)%(15*60)) as intervals from tablename
group by intervals

SEC_TO_TIME

返回将参数 seconds 转换为小时、分钟和秒数的时间值。

1
2
3
4
5
6
7
mysql> SELECT SEC_TO_TIME('54925');
+----------------------+
| SEC_TO_TIME('54925') |
+----------------------+
| 15:15:25 |
+----------------------+
1 row in set (0.00 sec)

DATE_FORMAT

用于以不同的格式显示日期/时间数据: DATE_FORMAT(date, format)

1
2
3
4
5
6
7
8
9
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')

Dec 29 2008 11:45 PM
12-29-2008
29 Dec 08
29 Dec 2008 16:25:46.635

Author: Mrli

Link: https://nymrli.top/2021/12/26/开发过程中Mysql-API记录/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
Kotlin集合、流式操作
NextPost >
Python科学计算常用API记录
CATALOG
  1. 1. 时间操作
    1. 1.1. TIME_TO_SEC
    2. 1.2. SEC_TO_TIME
    3. 1.3. DATE_FORMAT