本文共 8603 字,大约阅读时间需要 28 分钟。
MySQL 提供了丰富的日期和时间函数,能够方便地处理日期和时间值。这些函数涵盖了从获取当前日期和时间到日期和时间的转换、计算以及格式化等多个方面,极大地提升了日期和时间操作的效率。本文将详细介绍这些函数的使用方法和示例。
curdate()
和 current_date()
yyyy-mm-dd
或 yyyymmdd
。mysql> select curdate(), current_date(), curdate() + 0;+------------+----------------+---------------+| curdate() | current_date() | curdate() + 0 |+------------+----------------+---------------+| 2022-07-12 | 2022-07-12 | 20220712 |+------------+----------------+---------------+
curtime()
和 current_time()
hh:mm:ss
或 hhmmss
。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_TIMESTAMP(date)
1970-01-01 00:00:00
GMT 开始的秒数)。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_date()
和 utc_time()
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/