博客
关于我
mysql函数汇总之日期和时间函数
阅读量:789 次
发布时间:2023-02-11

本文共 8603 字,大约阅读时间需要 28 分钟。

MySQL 日期和时间函数详解

MySQL 提供了丰富的日期和时间函数,能够方便地处理日期和时间值。这些函数涵盖了从获取当前日期和时间到日期和时间的转换、计算以及格式化等多个方面,极大地提升了日期和时间操作的效率。本文将详细介绍这些函数的使用方法和示例。


获取当前日期的函数

curdate()current_date()

  • 作用:返回当前日期,格式为 yyyy-mm-ddyyyymmdd
  • 示例
    mysql> select curdate(), current_date(), curdate() + 0;+------------+----------------+---------------+| curdate()  | current_date() | curdate() + 0 |+------------+----------------+---------------+| 2022-07-12 | 2022-07-12     |      20220712 |+------------+----------------+---------------+

获取当前时间的函数

curtime()current_time()

  • 作用:返回当前时间,格式为 hh:mm:sshhmmss
  • 示例
    mysql> select curtime(), current_time(), curtime() + 0;+-----------+----------------+-------------+| curtime() | current_time() | curtime() + 0 |+-----------+----------------+-------------+| 21:32:25  | 21:32:25       |      213225 |+-----------+----------------+-------------+

获取当前日期和时间的函数

current_timestamp(), localtime(), now(), sysdate()

  • 作用:返回当前日期和时间的值。
  • 示例
    mysql> select current_timestamp(), localtime(), now(), sysdate();+---------------------+---------------------+---------------------+---------------------+| current_timestamp() | localtime()         | now()               | sysdate()           |+---------------------+---------------------+---------------------+---------------------+| 2022-07-12 21:34:52 | 2022-07-12 21:34:52 | 2022-07-12 21:34:52 | 2022-07-12 21:34:52 |+---------------------+---------------------+---------------------+---------------------+

UNIX 时间戳函数

UNIX_TIMESTAMP(date)

  • 作用:返回一个 UNIX 时间戳(从 1970-01-01 00:00:00 GMT 开始的秒数)。
  • 说明
    • 若无参数,默认返回当前时间的 UNIX 时间戳。
    • 参数可为 DATE 字符串、DATETIME 字符串、TIMESTAMP 或年月日格式数字。
  • 示例
    mysql> select unix_timestamp(), unix_timestamp(now()), now();+------------------+-----------------------+---------------------+| unix_timestamp() | unix_timestamp(now()) | now()               |+------------------+-----------------------+---------------------+|       1657633074 |            1657633074 | 2022-07-12 21:37:54 |+------------------+-----------------------+---------------------+

返回 UTC 日期和时间的函数

utc_date()utc_time()

  • 作用:返回当前 UTC(世界标准时间)的日期和时间。
  • 示例
    mysql> select utc_date(), utc_date() + 0;+------------+--------------+| utc_date() | utc_date() + 0 |+------------+--------------+| 2022-07-12 |     20220712 |+------------+--------------+

获取月份和星期的函数

month(date)monthname(date)

  • 作用:返回日期对应的月份和月份英文名称。

  • 示例

    mysql> select month('2022-12-12') as coll, month('20221212') as coll_1, month('221212') as coll_2;+------+--------+--------+| coll | coll_1 | coll_2 |+------+--------+--------+|   12 |     12 |     12 |+------+--------+--------+
    mysql> select monthname('2022-12-12'), monthname('20221212'), monthname('221212');+-------------------------+-----------------------+---------------------+| monthname('2022-12-12') | monthname('20221212') | monthname('221212') |+-------------------------+-----------------------+---------------------+| December                | December              | December            |+-------------------------+-----------------------+---------------------+

获取星期的函数

dayname(date), dayofweek(date), weekday(date)

  • 作用:返回日期对应的星期信息。

  • 示例

    mysql> select dayname('2022-07-12');+-----------------------+| dayname('2022-07-12') |+-----------------------+| Tuesday               |+-----------------------+
    mysql> select dayofweek('2022-07-12') as coll, dayofweek('2022-07-13') as coll_1;+------+--------+| coll | coll_1 |+------+--------+|    3 |      4 |+------+--------+
    mysql> select weekday('2022-07-12') as coll, weekday('2022-07-13') as coll_1;+------+--------+| coll | coll_1 |+------+--------+|    1 |      2 |+------+--------+

获取星期的函数(续)

week(date)weekofyear(date)

  • 作用:计算日期对应的一年中的星期数。

  • 示例

    mysql> select week('2022-07-13'), week('2022-01-01'), week('2022-09-18');+--------------------+--------------------+--------------------+| week('2022-07-13') | week('2022-01-01') | week('2022-09-18') |+--------------------+--------------------+--------------------+|                 28 |                  0 |                 38 |+--------------------+--------------------+--------------------+
    mysql> select weekofyear('2022-07-13'), week('2022-07-13', 3);+--------------------------+-----------------------+| weekofyear('2022-07-13') | week('2022-07-13', 3) |+--------------------------+-----------------------+|                       28 |                    28 |+--------------------------+-----------------------+

获取天数的函数

dayofyear(date)dayofmonth(date)

  • 作用:返回日期对应的一年中的天数和月份中的天数。

  • 示例

    mysql> select dayofyear('2022-07-13'), dayofyear('2022-01-01');+-------------------------+-------------------------+| dayofyear('2022-07-13') | dayofyear('2022-01-01') |+-------------------------+-------------------------+|                     194 |                       1 |+-------------------------+-------------------------+
    mysql> select dayofmonth('2022-07-13'), dayofmonth('220713'), dayofmonth('0713');+--------------------------+----------------------+--------------------+| dayofmonth('2022-07-13') | dayofmonth('220713') | dayofmonth('0713') |+--------------------------+----------------------+--------------------+|                       13 |                   13 |               NULL |+--------------------------+----------------------+--------------------+

获取年份、季度、小时、分钟和秒钟的函数

year(date), quarter(date), minute(time), second(time)

  • 作用:分别返回日期和时间的年份、季度、分钟和秒数。

  • 示例

    mysql> select year('2022-07-13'), year('20330909');+--------------------+------------------+| year('2022-07-13') | year('20330909') |+--------------------+------------------+|               2022 |             2033 |+--------------------+------------------+
    mysql> select quarter('2022-07-13'), quarter('20330101');+-----------------------+---------------------+| quarter('2022-07-13') | quarter('20330101') |+-----------------------+---------------------+|                     3 |                   1 |+-----------------------+---------------------+

获取日期的指定值的函数

extract(type from date)

  • 作用:从日期中提取指定类型的值。
  • 示例
    mysql> select extract(year from '2022-07-13') as coll, extract(year_month from '2022-07-13') as coll_1, extract(day_minute from '2022-07-13 09:08:07') as coll_2;+------+--------+--------+| coll | coll_1 | coll_2 |+------+--------+--------+| 2022 | 202207 | 130908 |+------+--------+--------+

时间和秒钟转换的函数

time_to_sec(time)sec_to_time(seconds)

  • 作用:将时间转换为秒数或将秒数转换为时间。
  • 示例
    mysql> select time_to_sec('09:09:09'), sec_to_time(32949);+-------------------------+--------------------+| time_to_sec('09:09:09') | sec_to_time(32949) |+-------------------------+--------------------+|                   32949 | 09:09:09           |+-------------------------+--------------------+

计算日期和时间的函数

date_add(), adddate(), date_sub(), subdate(), addtime(), subtime(), datediff()

  • 作用:执行日期和时间的加、减运算或计算。

  • 示例

    mysql> select date_add('2022-07-13 09:09:09', interval 1 second) as coll, adddate('2022-07-13 09:09:09', interval 1 second) as coll_1, date_add('2022-07-13 09:09:09', interval '1:1' minute_second) as coll_2;+---------------------+---------------------+---------------------+| coll                | coll_1              | coll_2              |+---------------------+---------------------+---------------------+| 2022-07-13 09:09:10 | 2022-07-13 09:09:10 | 2022-07-13 09:10:10 |+---------------------+---------------------+---------------------+
    mysql> select date_sub('2020-07-13 09:09:09', interval 31 day) as coll, subdate('2022-07-13 09:09:09', interval 31 day) as coll_1, date_sub('2022-07-13 09:09:09', interval '0 0:1:1' day_second) as coll_2;+---------------------+---------------------+---------------------+| coll                | coll_1              | coll_2              |+---------------------+---------------------+---------------------+| 2020-07-13 09:09:09 | 2022-07-13 09:09:09 | 2022-07-13 09:08:08 |+---------------------+---------------------+---------------------+

将日期和时间格式化的函数

date_format(date, format)time_format(time, format)

  • 作用:根据指定格式格式化日期和时间。

  • 示例

    mysql> select date_format('2022-07-13 09:08:07', '%W %M %Y') as coll, date_format('2022-07-13 09:08:07', '%D %y %a %d %m %b %j') as coll_1;+---------------------+---------------------------+| coll                | coll_1                    |+---------------------+---------------------------+| Wednesday July 2022 | 13th 22 Wed 13 07 Jul 194 |+---------------------+---------------------------+
    mysql> select time_format('13:14:15', '%H %k %h %I %l');+-------------------------------------------+| time_format('13:14:15', '%H %k %h %I %l') |+-------------------------------------------+| 13 13 01 01 1                             |+-------------------------------------------+

通过以上函数,您可以灵活地处理日期和时间数据,完成多种复杂任务。MySQL 的日期和时间函数功能强大,能够满足大多数数据库应用需求。

转载地址:http://gobfk.baihongyu.com/

你可能感兴趣的文章
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>