板凳-------Mysql cookbook学习 (十--4)

6.3 设置客户端时区

--客户端位于不同时区需要注意,如果位于同一时区则不需要关心
mysql> drop table if exists t;
Query OK, 0 rows affected (0.06 sec)mysql> create table t (ts timestamp);
Query OK, 0 rows affected (0.05 sec)mysql> insert into t (ts) values('2006-06-01 12:30:00');
Query OK, 1 row affected (0.01 sec)mysql> select ts from t;
+---------------------+
| ts                  |
+---------------------+
| 2006-06-01 12:30:00 |
+---------------------+
1 row in set (0.00 sec)Windows 中类似date命令的用法
如果需要查看日期时间,可在 CMD 中输入:
cmd
date /t  # 仅显示日期(如 2025年06月10日)
time /t  # 仅显示时间(如 14:30)mysql> SET SESSION time_zone = '+06:00';
Query OK, 0 rows affected (0.00 sec)mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM             | +06:00              |
+--------------------+---------------------+
1 row in set (0.00 sec)mysql> select ts from t;
+---------------------+
| ts                  |
+---------------------+
| 2006-06-01 10:30:00 |
+---------------------+
1 row in set (0.00 sec)mysql> set session time_zone = '+04:00';
Query OK, 0 rows affected (0.00 sec)mysql> select @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM             | +04:00              |
+--------------------+---------------------+
1 row in set (0.00 sec)mysql> select ts from t;
+---------------------+
| ts                  |
+---------------------+
| 2006-06-01 08:30:00 |
+---------------------+
1 row in set (0.00 sec)

6.4 获取当前日期或时间

mysql> select curdate(), curtime(), now();
+------------+-----------+---------------------+
| curdate()  | curtime() | now()               |
+------------+-----------+---------------------+
| 2025-06-10 | 15:43:19  | 2025-06-10 15:43:19 |
+------------+-----------+---------------------+
1 row in set (0.00 sec)mysql> select current_date(), current_time(), current_timestamp();
+----------------+----------------+---------------------+
| current_date() | current_time() | current_timestamp() |
+----------------+----------------+---------------------+
| 2025-06-10     | 15:43:44       | 2025-06-10 15:43:44 |
+----------------+----------------+---------------------+
1 row in set (0.00 sec)mysql> select utc_date(), utc_time(), utc_timestamp();
+------------+------------+---------------------+
| utc_date() | utc_time() | utc_timestamp()     |
+------------+------------+---------------------+
| 2025-06-10 | 11:44:10   | 2025-06-10 11:44:10 |
+------------+------------+---------------------+
1 row in set (0.00 sec)

6.5 使用timestamp来跟踪行修改时间

--timestamp初始化为当前日期,并随行值改变而改变
mysql> drop table if exists t;
Query OK, 0 rows affected (0.04 sec)mysql> create table t(ts timestamp);
Query OK, 0 rows affected (0.05 sec)mysql> show create table t\G
*************************** 1. row ***************************Table: t
Create Table: CREATE TABLE `t` (`ts` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)mysql> drop table if exists tsdemo1;
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> create table tsdemo1(ts timestamp, val int);
Query OK, 0 rows affected (0.05 sec)mysql> insert into tsdemo1(val) values(5);
Query OK, 1 row affected (0.01 sec)mysql> insert into tsdemo1(ts, val) values(null, 10);
Query OK, 1 row affected (0.01 sec)mysql> select * from tsdemo1;
+------+------+
| ts   | val  |
+------+------+
| NULL |    5 |
| NULL |   10 |
+------+------+
2 rows in set (0.00 sec)mysql> update tsdemo1 set val=6 where val=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from tsdemo1;
+------+------+
| ts   | val  |
+------+------+
| NULL |    6 |
| NULL |   10 |
+------+------+
2 rows in set (0.00 sec)mysql> update tsdemo1 set val= val+1;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0--timestamp初始化为当前日期,行值改变不改变
mysql> drop table if exists tsdemo2;
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> create table tsdemo2(t_create timestamp default current_timestamp, val int);
Query OK, 0 rows affected (0.06 sec)mysql> select * from tsdemo2;
Empty set (0.00 sec)mysql> insert into tsdemo2(val) values(5);
Query OK, 1 row affected (0.01 sec)mysql> insert into tsdemo2(t_create, val) values(null, 10);
Query OK, 1 row affected (0.01 sec)mysql> select * from tsdemo2;
+---------------------+------+
| t_create            | val  |
+---------------------+------+
| 2025-06-10 15:53:44 |    5 |
| NULL                |   10 |
+---------------------+------+
2 rows in set (0.00 sec)mysql> update tsdemo2 set val = val+1;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0mysql> select * from tsdemo2;
+---------------------+------+
| t_create            | val  |
+---------------------+------+
| 2025-06-10 15:53:44 |    6 |
| NULL                |   11 |
+---------------------+------+
2 rows in set (0.00 sec)

6.6 从日期或者时间值中分解出各部分值

--使用成分分解函数来分解日期和时间值
mysql> select dt, date(dt), time(dt) from datetime_val;
+---------------------+------------+----------+
| dt                  | date(dt)   | time(dt) |
+---------------------+------------+----------+
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
+---------------------+------------+----------+
8 rows in set (0.01 sec)--日期相关函数
函数                                返回值
year()                            月份数值    
month()                            月份名称(1..12)            
monthname()                    月份中的天数值(January..December)
dayofmonth()                    一周中的天数(1..31)
dayname()                        一周中的天数(Sunday..Saturday)
dayofweek()                    一周中的天数(1..7对应Sunday..Saturday)
weekday()                        一周中的天数(0..6对应Monday..Sunday)
dayofyear()                    一年中的天数值(1.366hour()                            时间中的小数值(0..23minute()                        时间中的分数值(0..59second()                        时间中的秒数(0..59)mysql> select dt,-> year(dt), dayofmonth(dt),-> hour(dt), second(dt)-> from datetime_val;
+---------------------+----------+----------------+----------+------------+
| dt                  | year(dt) | dayofmonth(dt) | hour(dt) | second(dt) |
+---------------------+----------+----------------+----------+------------+
| 1970-01-01 00:00:00 |     1970 |              1 |        0 |          0 |
| 1999-12-31 09:00:00 |     1999 |             31 |        9 |          0 |
| 2000-06-04 15:45:30 |     2000 |              4 |       15 |         30 |
| 2017-03-16 12:30:15 |     2017 |             16 |       12 |         15 |
| 1970-01-01 00:00:00 |     1970 |              1 |        0 |          0 |
| 1999-12-31 09:00:00 |     1999 |             31 |        9 |          0 |
| 2000-06-04 15:45:30 |     2000 |              4 |       15 |         30 |
| 2017-03-16 12:30:15 |     2017 |             16 |       12 |         15 |
+---------------------+----------+----------------+----------+------------+
8 rows in set (0.01 sec)mysql> select d, dayofyear(d) from date_val;
+------------+--------------+
| d          | dayofyear(d) |
+------------+--------------+
| 1864-02-28 |           59 |
| 1900-01-15 |           15 |
| 1999-12-31 |          365 |
| 2000-06-04 |          156 |
| 2017-03-16 |           75 |
| 1864-02-28 |           59 |
| 1900-01-15 |           15 |
| 1999-12-31 |          365 |
| 2000-06-04 |          156 |
| 2017-03-16 |           75 |
| 2007-05-13 |          133 |
+------------+--------------+
11 rows in set (0.00 sec)mysql> select d, dayname(d),left(dayname(d), 3) from date_val;
+------------+------------+---------------------+
| d          | dayname(d) | left(dayname(d), 3) |
+------------+------------+---------------------+
| 1864-02-28 | Sunday     | Sun                 |
| 1900-01-15 | Monday     | Mon                 |
| 1999-12-31 | Friday     | Fri                 |
| 2000-06-04 | Sunday     | Sun                 |
| 2017-03-16 | Thursday   | Thu                 |
| 1864-02-28 | Sunday     | Sun                 |
| 1900-01-15 | Monday     | Mon                 |
| 1999-12-31 | Friday     | Fri                 |
| 2000-06-04 | Sunday     | Sun                 |
| 2017-03-16 | Thursday   | Thu                 |
| 2007-05-13 | Sunday     | Sun                 |
+------------+------------+---------------------+
11 rows in set (0.00 sec)mysql> select d, dayname(d),dayofweek(d), weekday(d) from date_val;
+------------+------------+--------------+------------+
| d          | dayname(d) | dayofweek(d) | weekday(d) |
+------------+------------+--------------+------------+
| 1864-02-28 | Sunday     |            1 |          6 |
| 1900-01-15 | Monday     |            2 |          0 |
| 1999-12-31 | Friday     |            6 |          4 |
| 2000-06-04 | Sunday     |            1 |          6 |
| 2017-03-16 | Thursday   |            5 |          3 |
| 1864-02-28 | Sunday     |            1 |          6 |
| 1900-01-15 | Monday     |            2 |          0 |
| 1999-12-31 | Friday     |            6 |          4 |
| 2000-06-04 | Sunday     |            1 |          6 |
| 2017-03-16 | Thursday   |            5 |          3 |
| 2007-05-13 | Sunday     |            1 |          6 |
+------------+------------+--------------+------------+
11 rows in set (0.00 sec)mysql> select dt, extract(day from dt), extract(hour from dt) from datetime_val;
+---------------------+----------------------+-----------------------+
| dt                  | extract(day from dt) | extract(hour from dt) |
+---------------------+----------------------+-----------------------+
| 1970-01-01 00:00:00 |                    1 |                     0 |
| 1999-12-31 09:00:00 |                   31 |                     9 |
| 2000-06-04 15:45:30 |                    4 |                    15 |
| 2017-03-16 12:30:15 |                   16 |                    12 |
| 1970-01-01 00:00:00 |                    1 |                     0 |
| 1999-12-31 09:00:00 |                   31 |                     9 |
| 2000-06-04 15:45:30 |                    4 |                    15 |
| 2017-03-16 12:30:15 |                   16 |                    12 |
+---------------------+----------------------+-----------------------+
8 rows in set (0.00 sec)mysql> select curdate(), year(curdate()) as year, month(curdate()) as month,-> monthname(curdate()) as monthnaem, dayofmonth(curdate()) as day,-> dayname(curdate()) as dayname;
+------------+------+-------+-----------+------+---------+
| curdate()  | year | month | monthnaem | day  | dayname |
+------------+------+-------+-----------+------+---------+
| 2025-06-10 | 2025 |     6 | June      |   10 | Tuesday |
+------------+------+-------+-----------+------+---------+
1 row in set (0.00 sec)mysql> select now(), hour(now()) as hour, minute(now()) as minute, second(now()) as second;
+---------------------+------+--------+--------+
| now()               | hour | minute | second |
+---------------------+------+--------+--------+
| 2025-06-10 16:06:16 |   16 |      6 |     16 |
+---------------------+------+--------+--------+
1 row in set (0.00 sec)--使用格式化分解日期或者时间值
mysql> select dt, date_format(dt, '%Y') as year, date_format(dt, '%d') as day, time_format(dt, '%H') as hour, time_format(dt, '%s') as second from datetime_val;
+---------------------+------+------+------+--------+
| dt                  | year | day  | hour | second |
+---------------------+------+------+------+--------+
| 1970-01-01 00:00:00 | 1970 | 01   | 00   | 00     |
| 1999-12-31 09:00:00 | 1999 | 31   | 09   | 00     |
| 2000-06-04 15:45:30 | 2000 | 04   | 15   | 30     |
| 2017-03-16 12:30:15 | 2017 | 16   | 12   | 15     |
| 1970-01-01 00:00:00 | 1970 | 01   | 00   | 00     |
| 1999-12-31 09:00:00 | 1999 | 31   | 09   | 00     |
| 2000-06-04 15:45:30 | 2000 | 04   | 15   | 30     |
| 2017-03-16 12:30:15 | 2017 | 16   | 12   | 15     |
+---------------------+------+------+------+--------+
8 rows in set (0.00 sec)mysql> select dt, date_format(dt, '%Y-%m-%d') as ' date part', time_format(dt, '%T') as' time part' from datetime_val;
+---------------------+------------+-----------+
| dt                  | date part  | time part |
+---------------------+------------+-----------+
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00  |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00  |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30  |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15  |
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00  |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00  |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30  |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15  |
+---------------------+------------+-----------+
8 rows in set, 2 warnings (0.00 sec)mysql> select ts, date_format(ts, '%M %e %Y') as 'descriptive date', time_format(ts, '%H:%i') as'hours/minutes' from timestamp_val;
+---------------------+------------------+---------------+
| ts                  | descriptive date | hours/minutes |
+---------------------+------------------+---------------+
| 1970-01-01 05:00:00 | January 1 1970   | 05:00         |
| 1987-03-05 08:30:15 | March 5 1987     | 08:30         |
| 1999-12-31 05:00:00 | December 31 1999 | 05:00         |
| 2000-06-04 11:45:30 | June 4 2000      | 11:45         |
+---------------------+------------------+---------------+
4 rows in set (0.00 sec)--使用字符串函数分解时间或者日期值
mysql> select dt, left(dt, 4) as year, mid(dt, 9, 2) as day, right(dt, 2) as second from datetime_val;
+---------------------+------+------+--------+
| dt                  | year | day  | second |
+---------------------+------+------+--------+
| 1970-01-01 00:00:00 | 1970 | 01   | 00     |
| 1999-12-31 09:00:00 | 1999 | 31   | 00     |
| 2000-06-04 15:45:30 | 2000 | 04   | 30     |
| 2017-03-16 12:30:15 | 2017 | 16   | 15     |
| 1970-01-01 00:00:00 | 1970 | 01   | 00     |
| 1999-12-31 09:00:00 | 1999 | 31   | 00     |
| 2000-06-04 15:45:30 | 2000 | 04   | 30     |
| 2017-03-16 12:30:15 | 2017 | 16   | 15     |
+---------------------+------+------+--------+
8 rows in set (0.00 sec)mysql> select dt, left(dt, 10) as date, right(dt, 8) as time from datetime_val;
+---------------------+------------+----------+
| dt                  | date       | time     |
+---------------------+------------+----------+
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
+---------------------+------------+----------+
8 rows in set (0.00 sec)mysql> select ts, left(ts, 10) as date, right(ts, 8) as time from timestamp_val;
+---------------------+------------+----------+
| ts                  | date       | time     |
+---------------------+------------+----------+
| 1970-01-01 05:00:00 | 1970-01-01 | 05:00:00 |
| 1987-03-05 08:30:15 | 1987-03-05 | 08:30:15 |
| 1999-12-31 05:00:00 | 1999-12-31 | 05:00:00 |
| 2000-06-04 11:45:30 | 2000-06-04 | 11:45:30 |
+---------------------+------------+----------+
4 rows in set (0.00 sec)

6.7 合成日期或者时间值

mysql> select maketime(10,30,58), maketime(-5,0,11);
+--------------------+-------------------+
| maketime(10,30,58) | maketime(-5,0,11) |
+--------------------+-------------------+
| 10:30:58           | -05:00:11         |
+--------------------+-------------------+
1 row in set (0.00 sec)mysql> select makedate(2007, 60);
+--------------------+
| makedate(2007, 60) |
+--------------------+
| 2007-03-01         |
+--------------------+
1 row in set (0.00 sec)mysql> select d, date_format(d, '%Y-%m-01') from date_val;
+------------+----------------------------+
| d          | date_format(d, '%Y-%m-01') |
+------------+----------------------------+
| 1864-02-28 | 1864-02-01                 |
| 1900-01-15 | 1900-01-01                 |
| 1999-12-31 | 1999-12-01                 |
| 2000-06-04 | 2000-06-01                 |
| 2017-03-16 | 2017-03-01                 |
| 1864-02-28 | 1864-02-01                 |
| 1900-01-15 | 1900-01-01                 |
| 1999-12-31 | 1999-12-01                 |
| 2000-06-04 | 2000-06-01                 |
| 2017-03-16 | 2017-03-01                 |
| 2007-05-13 | 2007-05-01                 |
+------------+----------------------------+
11 rows in set (0.00 sec)mysql> select t1, time_format(t1, '%H:%i:00' ) from time_val;
+----------+------------------------------+
| t1       | time_format(t1, '%H:%i:00' ) |
+----------+------------------------------+
| 15:00:00 | 15:00:00                     |
| 05:01:30 | 05:01:00                     |
| 12:30:20 | 12:30:00                     |
+----------+------------------------------+
3 rows in set (0.00 sec)mysql> select d, concat(year(d),'-', month(d), '-01') from date_val;
+------------+--------------------------------------+
| d          | concat(year(d),'-', month(d), '-01') |
+------------+--------------------------------------+
| 1864-02-28 | 1864-2-01                            |
| 1900-01-15 | 1900-1-01                            |
| 1999-12-31 | 1999-12-01                           |
| 2000-06-04 | 2000-6-01                            |
| 2017-03-16 | 2017-3-01                            |
| 1864-02-28 | 1864-2-01                            |
| 1900-01-15 | 1900-1-01                            |
| 1999-12-31 | 1999-12-01                           |
| 2000-06-04 | 2000-6-01                            |
| 2017-03-16 | 2017-3-01                            |
| 2007-05-13 | 2007-5-01                            |
+------------+--------------------------------------+
11 rows in set (0.00 sec)mysql> select d, concat(year(d), '-', lpad(month(d), 2, '0'), '-01') from date_val;
+------------+-----------------------------------------------------+
| d          | concat(year(d), '-', lpad(month(d), 2, '0'), '-01') |
+------------+-----------------------------------------------------+
| 1864-02-28 | 1864-02-01                                          |
| 1900-01-15 | 1900-01-01                                          |
| 1999-12-31 | 1999-12-01                                          |
| 2000-06-04 | 2000-06-01                                          |
| 2017-03-16 | 2017-03-01                                          |
| 1864-02-28 | 1864-02-01                                          |
| 1900-01-15 | 1900-01-01                                          |
| 1999-12-31 | 1999-12-01                                          |
| 2000-06-04 | 2000-06-01                                          |
| 2017-03-16 | 2017-03-01                                          |
| 2007-05-13 | 2007-05-01                                          |
+------------+-----------------------------------------------------+
11 rows in set (0.00 sec)mysql> select d, concat(year(d), '-', lpad(month(d), 4, '0'), '-01') from date_val;
+------------+-----------------------------------------------------+
| d          | concat(year(d), '-', lpad(month(d), 4, '0'), '-01') |
+------------+-----------------------------------------------------+
| 1864-02-28 | 1864-0002-01                                        |
| 1900-01-15 | 1900-0001-01                                        |
| 1999-12-31 | 1999-0012-01                                        |
| 2000-06-04 | 2000-0006-01                                        |
| 2017-03-16 | 2017-0003-01                                        |
| 1864-02-28 | 1864-0002-01                                        |
| 1900-01-15 | 1900-0001-01                                        |
| 1999-12-31 | 1999-0012-01                                        |
| 2000-06-04 | 2000-0006-01                                        |
| 2017-03-16 | 2017-0003-01                                        |
| 2007-05-13 | 2007-0005-01                                        |
+------------+-----------------------------------------------------+
11 rows in set (0.00 sec)mysql> select t1, concat(lpad(hour(t1), 2, '0'), ':', lpad(minute(t1), 2, '0'), ':00') as recombined from time_val;
+----------+------------+
| t1       | recombined |
+----------+------------+
| 15:00:00 | 15:00:00   |
| 05:01:30 | 05:01:00   |
| 12:30:20 | 12:30:00   |
+----------+------------+
3 rows in set (0.00 sec)mysql> set @d= '2006-02-28';
Query OK, 0 rows affected (0.00 sec)mysql> set @t='13:10:05';
Query OK, 0 rows affected (0.00 sec)mysql> SELECT @d, @t, CONCAT(@d, ' ', @t) AS datetime_combined;
+------------+----------+---------------------+
| @d         | @t       | datetime_combined   |
+------------+----------+---------------------+
| 2006-02-28 | 13:10:05 | 2006-02-28 13:10:05 |
+------------+----------+---------------------+
1 row in set (0.00 sec)mysql> SELECT CONCAT_WS(' ', @d, @t);
+------------------------+
| CONCAT_WS(' ', @d, @t) |
+------------------------+
| 2006-02-28 13:10:05    |
+------------------------+
1 row in set (0.00 sec)

6.8 在时间数据类型和基本单位间进行转换

--在时间和秒之间进行转换
mysql> select t1, time_to_sec(t1) as 'TIME to seconds', sec_to_time(time_to_sec(t1)) as 'TIME to seconds to TIME' from time_val;
+----------+-----------------+-------------------------+
| t1       | TIME to seconds | TIME to seconds to TIME |
+----------+-----------------+-------------------------+
| 15:00:00 |           54000 | 15:00:00                |
| 05:01:30 |           18090 | 05:01:30                |
| 12:30:20 |           45020 | 12:30:20                |
+----------+-----------------+-------------------------+
3 rows in set (0.00 sec)mysql> select t1, time_to_sec(t1) as 'seconds', time_to_sec(t1)/60 as 'minutes', time_to_sec(t1)/(60*60) as hours, time_to_sec(t1)/(24*60*60) as 'days' from time_val;
+----------+---------+----------+---------+--------+
| t1       | seconds | minutes  | hours   | days   |
+----------+---------+----------+---------+--------+
| 15:00:00 |   54000 | 900.0000 | 15.0000 | 0.6250 |
| 05:01:30 |   18090 | 301.5000 |  5.0250 | 0.2094 |
| 12:30:20 |   45020 | 750.3333 | 12.5056 | 0.5211 |
+----------+---------+----------+---------+--------+
3 rows in set (0.00 sec)mysql> select t1, time_to_sec(t1) as 'seconds',->         floor(time_to_sec(t1)/60) as 'minutes',->         floor(time_to_sec(t1)/(60*60)) as hours,->         floor(time_to_sec(t1)/(24*60*60)) as 'days'-> from time_val;
+----------+---------+---------+-------+------+
| t1       | seconds | minutes | hours | days |
+----------+---------+---------+-------+------+
| 15:00:00 |   54000 |     900 |    15 |    0 |
| 05:01:30 |   18090 |     301 |     5 |    0 |
| 12:30:20 |   45020 |     750 |    12 |    0 |
+----------+---------+---------+-------+------+
3 rows in set (0.00 sec)mysql> select dt, time_to_sec(dt) as 'time part in seconds',->         sec_to_time(time_to_sec(dt)) as 'time part as TIME'-> from datetime_val;
+---------------------+----------------------+-------------------+
| dt                  | time part in seconds | time part as TIME |
+---------------------+----------------------+-------------------+
| 1970-01-01 00:00:00 |                    0 | 00:00:00          |
| 1999-12-31 09:00:00 |                32400 | 09:00:00          |
| 2000-06-04 15:45:30 |                56730 | 15:45:30          |
| 2017-03-16 12:30:15 |                45015 | 12:30:15          |
| 1970-01-01 00:00:00 |                    0 | 00:00:00          |
| 1999-12-31 09:00:00 |                32400 | 09:00:00          |
| 2000-06-04 15:45:30 |                56730 | 15:45:30          |
| 2017-03-16 12:30:15 |                45015 | 12:30:15          |
+---------------------+----------------------+-------------------+
8 rows in set (0.00 sec)mysql> select ts, time_to_sec(ts) as 'time part in seconds',->         sec_to_time(time_to_sec(ts)) as 'time part as TIME'-> from timestamp_val;
+---------------------+----------------------+-------------------+
| ts                  | time part in seconds | time part as TIME |
+---------------------+----------------------+-------------------+
| 1970-01-01 05:00:00 |                18000 | 05:00:00          |
| 1987-03-05 08:30:15 |                30615 | 08:30:15          |
| 1999-12-31 05:00:00 |                18000 | 05:00:00          |
| 2000-06-04 11:45:30 |                42330 | 11:45:30          |
+---------------------+----------------------+-------------------+
4 rows in set (0.00 sec)--在日期值和天数之间进行转换
mysql> select d, to_days(d) as 'date to days', from_days(to_days(d)) as 'DATE to days to DATE' from date_val;
+------------+--------------+----------------------+
| d          | date to days | DATE to days to DATE |
+------------+--------------+----------------------+
| 1864-02-28 |       680870 | 1864-02-28           |
| 1900-01-15 |       693975 | 1900-01-15           |
| 1999-12-31 |       730484 | 1999-12-31           |
| 2000-06-04 |       730640 | 2000-06-04           |
| 2017-03-16 |       736769 | 2017-03-16           |
| 1864-02-28 |       680870 | 1864-02-28           |
| 1900-01-15 |       693975 | 1900-01-15           |
| 1999-12-31 |       730484 | 1999-12-31           |
| 2000-06-04 |       730640 | 2000-06-04           |
| 2017-03-16 |       736769 | 2017-03-16           |
| 2007-05-13 |       733174 | 2007-05-13           |
+------------+--------------+----------------------+
11 rows in set (0.00 sec)mysql> select dt, to_days(dt) as 'date part in days', from_days(to_days(dt)) as 'date part as DATE' from datetime_val;
+---------------------+-------------------+-------------------+
| dt                  | date part in days | date part as DATE |
+---------------------+-------------------+-------------------+
| 1970-01-01 00:00:00 |            719528 | 1970-01-01        |
| 1999-12-31 09:00:00 |            730484 | 1999-12-31        |
| 2000-06-04 15:45:30 |            730640 | 2000-06-04        |
| 2017-03-16 12:30:15 |            736769 | 2017-03-16        |
| 1970-01-01 00:00:00 |            719528 | 1970-01-01        |
| 1999-12-31 09:00:00 |            730484 | 1999-12-31        |
| 2000-06-04 15:45:30 |            730640 | 2000-06-04        |
| 2017-03-16 12:30:15 |            736769 | 2017-03-16        |
+---------------------+-------------------+-------------------+
8 rows in set (0.00 sec)mysql> select ts, to_days(ts) as 'date part in days', from_days(to_days(ts)) as 'date part as DATE' from timestamp_val;
+---------------------+-------------------+-------------------+
| ts                  | date part in days | date part as DATE |
+---------------------+-------------------+-------------------+
| 1970-01-01 05:00:00 |            719528 | 1970-01-01        |
| 1987-03-05 08:30:15 |            725800 | 1987-03-05        |
| 1999-12-31 05:00:00 |            730484 | 1999-12-31        |
| 2000-06-04 11:45:30 |            730640 | 2000-06-04        |
+---------------------+-------------------+-------------------+
4 rows in set (0.00 sec)
--在datetime或者timestamp类型值和秒数之间进行转换
mysql> select dt, unix_timestamp(dt) as seconds, from_unixtime(unix_timestamp(dt)) as timestamp from datetime_val;
+---------------------+------------+---------------------+
| dt                  | seconds    | timestamp           |
+---------------------+------------+---------------------+
| 1970-01-01 00:00:00 |          0 | 1970-01-01 04:00:00 |
| 1999-12-31 09:00:00 |  946616400 | 1999-12-31 09:00:00 |
| 2000-06-04 15:45:30 |  960119130 | 2000-06-04 15:45:30 |
| 2017-03-16 12:30:15 | 1489653015 | 2017-03-16 12:30:15 |
| 1970-01-01 00:00:00 |          0 | 1970-01-01 04:00:00 |
| 1999-12-31 09:00:00 |  946616400 | 1999-12-31 09:00:00 |
| 2000-06-04 15:45:30 |  960119130 | 2000-06-04 15:45:30 |
| 2017-03-16 12:30:15 | 1489653015 | 2017-03-16 12:30:15 |
+---------------------+------------+---------------------+
8 rows in set (0.00 sec)mysql> select curdate(), unix_timestamp(curdate()), from_unixtime(unix_timestamp(curdate()))\G
*************************** 1. row ***************************curdate(): 2025-06-10unix_timestamp(curdate()): 1749499200
from_unixtime(unix_timestamp(curdate())): 2025-06-10 00:00:00
1 row in set (0.00 sec)

6.9 计算两个日期或时间之间的间隔

--使用时间差函数
set @d1 = '2010-01-01', @d2 = '2009-12-01';
select datediff(@d1, @d2) as 'd1 - d2', datediff(@d2, @d1) as 'd2 - d1';set @t1 = '12:00:00', @t2 = '16:30:00';
select timediff(@t1, @t2) as 't1 - t2', timediff(@t2, @t1) as 't2 - t1';select t1, t2, timediff(t2, t1) as 't2 - t1 as TIME', if(timediff(t2, t1) >= 0, '+', '-') as sign,hour(timediff(t2, t1)) as hour,minute(timediff(t2, t1)) as minute,second(timediff(t2, t1)) as second
from time_val;set @dt1 = '1900-01-01 00:00:00', @dt2 = '1910-01-01 00:00:00';
select timestampdiff(minute, @dt1, @dt2) as minutes,timestampdiff(hour, @dt1, @dt2) as hours,timestampdiff(day, @dt1, @dt2) as days,timestampdiff(week, @dt1, @dt2) as weeks,timestampdiff(year, @dt1, @dt2) as years;--利用基本时间单位计算时间间隔
select t1, t2, time_to_sec(t2) - time_to_sec(t1) as 't2 - t1 (in seconds)',sec_to_time(time_to_sec(t2) - time_to_sec(t1)) as 't2 - t1 (as TIME)'
from time_val;--使用基本单位计算两个Date类型值,或者两个date-and-time类型值的时间间隔
select to_days('1884-01-01') - to_days('1883-06-05') as days;
select (to_days('1884-01-01') - to_days('1883-06-05')) / 7 as weeks;set @dt1 = '1984-01-01 09:00:00';
set @dt2 = @dt1 + interval 14 day;
select unix_timestamp(@dt2) - unix_timestamp(@dt1) as seconds;set @interval = unix_timestamp(@dt2) - unix_timestamp(@dt1);
select @interval as seconds,@interval / 60 as minutes,@interval / (60 * 60) as hours,@interval / (24 * 60 * 60) as days,@interval / (7 * 24 * 60 * 60) as weeks;set @dt1 = '1800-02-14 07:30:00';
set @dt2 = @dt1 + interval 7 day;
set @interval = ((to_days(@dt2) - to_days(@dt1)) * 24 * 60 * 60)+ time_to_sec(@dt2) - time_to_sec(@dt1);select @interval as seconds, sec_to_time(@interval) as time;

6.10 增加日期或时间值

--使用时间加法函数或者操作符进行时间值求和运算
mysql> set @t1 = '12:00:00', @t2 = '15:30:00';
Query OK, 0 rows affected (0.00 sec)mysql> select addtime(@t1, @t2);
+-------------------+
| addtime(@t1, @t2) |
+-------------------+
| 27:30:00          |
+-------------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '1984-03-01 12:00:00', @t = '12:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select addtime(@dt, @t);
+---------------------+
| addtime(@dt, @t)    |
+---------------------+
| 1984-03-02 00:00:00 |
+---------------------+
1 row in set (0.00 sec)mysql>
mysql> set @d = '1984-03-01', @t = '15:30:00';
Query OK, 0 rows affected (0.00 sec)mysql> select timestamp(@d, @t);
+----------------------------+
| timestamp(@d, @t)          |
+----------------------------+
| 1984-03-01 15:30:00.000000 |
+----------------------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '1984-03-01 12:00:00', @t = '12:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select timestamp(@dt, @t);
+----------------------------+
| timestamp(@dt, @t)         |
+----------------------------+
| 1984-03-02 00:00:00.000000 |
+----------------------------+
1 row in set (0.00 sec)--求出即日第三天的日期值
mysql> select curdate(), date_add(curdate(), interval 3 day);
+------------+-------------------------------------+
| curdate()  | date_add(curdate(), interval 3 day) |
+------------+-------------------------------------+
| 2025-06-10 | 2025-06-13                          |
+------------+-------------------------------------+
1 row in set (0.00 sec)
--一个星期前的日期值
mysql> select curdate(), date_sub(curdate(), interval 7 day);
+------------+-------------------------------------+
| curdate()  | date_sub(curdate(), interval 7 day) |
+------------+-------------------------------------+
| 2025-06-10 | 2025-06-03                          |
+------------+-------------------------------------+
1 row in set (0.00 sec)
--60小时之后是什么时间(同时需要日期和时间值)
mysql> select now(), date_add(now(), interval 60 hour);
+---------------------+-----------------------------------+
| now()               | date_add(now(), interval 60 hour) |
+---------------------+-----------------------------------+
| 2025-06-10 16:50:34 | 2025-06-13 04:50:34               |
+---------------------+-----------------------------------+
1 row in set (0.00 sec)mysql>
mysql> select now(), date_add(now(), interval '14:30' hour_minute);
+---------------------+-----------------------------------------------+
| now()               | date_add(now(), interval '14:30' hour_minute) |
+---------------------+-----------------------------------------------+
| 2025-06-10 16:50:34 | 2025-06-11 07:20:34                           |
+---------------------+-----------------------------------------------+
1 row in set (0.00 sec)mysql> select now(), date_add(now(), interval '3 4' day_hour);
+---------------------+------------------------------------------+
| now()               | date_add(now(), interval '3 4' day_hour) |
+---------------------+------------------------------------------+
| 2025-06-10 16:50:34 | 2025-06-13 20:50:34                      |
+---------------------+------------------------------------------+
1 row in set (0.00 sec)mysql> select curdate(), curdate() + interval 1 year;
+------------+-----------------------------+
| curdate()  | curdate() + interval 1 year |
+------------+-----------------------------+
| 2025-06-10 | 2026-06-10                  |
+------------+-----------------------------+
1 row in set (0.00 sec)mysql> select now(), now() - interval '1 12' day_hour;
+---------------------+----------------------------------+
| now()               | now() - interval '1 12' day_hour |
+---------------------+----------------------------------+
| 2025-06-10 16:50:36 | 2025-06-09 04:50:36              |
+---------------------+----------------------------------+
1 row in set (0.00 sec)--使用基本时间单位进行时间加法运算
mysql> select t1, sec_to_time(time_to_sec(t1) + 7200) as 't1 plus 2 hours' from time_val;
+----------+-----------------+
| t1       | t1 plus 2 hours |
+----------+-----------------+
| 15:00:00 | 17:00:00        |
| 05:01:30 | 07:01:30        |
| 12:30:20 | 14:30:20        |
+----------+-----------------+
3 rows in set (0.00 sec)mysql>
mysql> select t1, t2, time_to_sec(t1) + time_to_sec(t2) as 't1 + t2 (in seconds)',->         sec_to_time(time_to_sec(t1) + time_to_sec(t2)) as 't1 + t2 (as TIME)'-> from time_val;
+----------+----------+----------------------+-------------------+
| t1       | t2       | t1 + t2 (in seconds) | t1 + t2 (as TIME) |
+----------+----------+----------------------+-------------------+
| 15:00:00 | 15:00:00 |               108000 | 30:00:00          |
| 05:01:30 | 02:30:20 |                27110 | 07:31:50          |
| 12:30:20 | 17:30:45 |               108065 | 30:01:05          |
+----------+----------+----------------------+-------------------+
3 rows in set (0.00 sec)mysql>
mysql>
mysql> select t1, t2, mod(time_to_sec(t1) + time_to_sec(t2), 86400) as 't1 + t2 (in seconds)',->         sec_to_time(mod(time_to_sec(t1) + time_to_sec(t2), 86400)) as 't1 + t2 (as TIME)'-> from time_val;
+----------+----------+----------------------+-------------------+
| t1       | t2       | t1 + t2 (in seconds) | t1 + t2 (as TIME) |
+----------+----------+----------------------+-------------------+
| 15:00:00 | 15:00:00 |                21600 | 06:00:00          |
| 05:01:30 | 02:30:20 |                27110 | 07:31:50          |
| 12:30:20 | 17:30:45 |                21665 | 06:01:05          |
+----------+----------+----------------------+-------------------+
3 rows in set (0.00 sec)mysql>
mysql> set @d = '2006-01-01';
Query OK, 0 rows affected (0.00 sec)mysql> select @d as date, from_days(to_days(@d) + 7) as 'date + 1 week',->         from_days(to_days(@d) - 7) as 'date - 1 week';
+------------+---------------+---------------+
| date       | date + 1 week | date - 1 week |
+------------+---------------+---------------+
| 2006-01-01 | 2006-01-08    | 2005-12-25    |
+------------+---------------+---------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '2006-01-01 12:30:45';
Query OK, 0 rows affected (0.00 sec)mysql> select @dt as datetime,->         from_days(to_days(@dt) + 7) as 'datetime + 1 week',->         from_days(to_days(@dt) - 7) as 'datetime - 1 week';
+---------------------+-------------------+-------------------+
| datetime            | datetime + 1 week | datetime - 1 week |
+---------------------+-------------------+-------------------+
| 2006-01-01 12:30:45 | 2006-01-08        | 2005-12-25        |
+---------------------+-------------------+-------------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '2006-01-01 09:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select @dt as datetime,->         from_unixtime(unix_timestamp(@dt) + 3600) as 'datetime + 1 hour',->         from_unixtime(unix_timestamp(@dt) - 3600) as 'datetime - 1 hour';
+---------------------+----------------------------+----------------------------+
| datetime            | datetime + 1 hour          | datetime - 1 hour          |
+---------------------+----------------------------+----------------------------+
| 2006-01-01 09:00:00 | 2006-01-01 10:00:00.000000 | 2006-01-01 08:00:00.000000 |
+---------------------+----------------------------+----------------------------+
1 row in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/bicheng/84437.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何根据excel表生成sql的insert脚本

根据excel自带的vba宏进行操作 首先altF11 点击插入~模块 录取执行语句 Sub GenerateSQL()Dim lastRow As IntegerlastRow Cells(Rows.Count, 1).End(xlUp).RowFor i 2 To lastRow 假设第一行是标题Cells(i, "S").Value "INSERT INTO table_name (ID, RE…

React hook之useRef

React useRef 详解 useRef 是 React 提供的一个 Hook,用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途,下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…

开疆智能ModbusTCP转Canopen网关连接AB PLC与台达伺服通讯案例

本案例是罗克韦尔PLC通过开疆智能ModbusTCP转Canopen网关连接台达A2伺服的配置案例。 配置方法: 首先打开PLC配置软件“Studio5000”并新建项目导入通讯文件 对功能块进行设置 填写本地IP地址以及服务区IP地址以及寄存器 填写寄存器地址数量及使能 确认无误后将配置…

用 LoRA 对 Qwen2.5-VL 模型进行SFT - LoRA微调流程

用 LoRA 对 Qwen2.5-VL 模型进行SFT - LoRA微调流程 flyfish ┌──────────────────────────────────────────────────────────────────────────┐ │ 环境准备与启动 …

熵最小化Entropy Minimization (二): 案例实施

前面介绍了熵最小化、常用的权重函数汇总、半监督学习:低密度分离假设 (Low-Density Separation Assumption)、标签平滑、信息最大化等相关的知识点,本文采用一个MNIST10分类的数据集来进一步体会它们的效果。 案例实施 对比方法 纯监督学习方法&…

联邦学习聚合参数操作详解

联邦学习中常见的模型聚合操作,具体用于对来自多个客户端的模型更新进行聚合,以得到全局模型。在联邦学习框架下,多个客户端在本地训练各自的模型后,会将模型更新(通常是模型的权重)发送到中央服务器&#…

蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练

前文覆盖了 BLE 扫描的基础概念与经典问题蓝牙 BLE 扫描面试题大全(1):从基础到实战的深度解析-CSDN博客,但实际面试中,企业更关注候选人对复杂场景的应对能力(如多设备并发扫描、低功耗与高发现率的平衡)和前沿技术的…

基于Flask实现的医疗保险欺诈识别监测模型

基于Flask实现的医疗保险欺诈识别监测模型 项目截图 项目简介 社会医疗保险是国家通过立法形式强制实施,由雇主和个人按一定比例缴纳保险费,建立社会医疗保险基金,支付雇员医疗费用的一种医疗保险制度, 它是促进社会文明和进步的…

机房断电后 etcd 启动失败的排查与快速恢复实录

目录 机房断电后 etcd 启动失败的排查与快速恢复实录 背景与问题起因 报错分析 解决方案:删除 member 数据重新初始化 步骤 1:停止 etcd 容器或服务 步骤 2:删除 member 目录 步骤 3:重启 etcd 服务 附加提醒 总结与后续…

Linux上并行打包压缩工具

文章目录 Linux上并行打包压缩工具1. **Pigz (Parallel gzip)**2. **Pbzip2 (Parallel bzip2)**3. **Pixz (Parallel xz)**4. **Zstd (支持多线程)**5. **GNU Parallel 传统工具**6. **Fastest Tools (综合建议)**注意事项: tar和zstd多线程压缩和解压**1. 多线程压…

【K8S系列】K8S中Service 无法访问问题的深度分析

摘要 : 本文是关于 Kubernetes 中 Service 无法访问问题的深度分析,结合根本原因、诊断策略与解决方案的系统性指南: Kubernetes Service 访问故障全景解析:从底层机制到实战修复 Service 作为 Kubernetes 集群内网络通信的核心抽…

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…

进行性核上性麻痹护理指南:全维度守护健康

进行性核上性麻痹是一种罕见的神经系统退行性疾病,会导致患者出现运动障碍、吞咽困难、认知障碍等症状。科学的健康护理对延缓病情发展、提升患者生活质量至关重要。 饮食护理:由于患者常存在吞咽困难,食物应选择糊状、软烂的类型&#xff0c…

【浅析赛题,国赛一等奖水平:思路+模型:】2025 年第八届河北省研究生数学建模竞赛:A 题 基于图论的复杂网络分析与可视化建模

问题一:社交网络拓扑结构分析与影响力评估 基础模型 有向加权图构建:将用户设为节点,互动关系为有向边,以互动频率(如一定时间内点赞、评论、转发次数)或加权求和(赋予不同互动类型不同权重&a…

web3-去中心化金融深度剖析:DEX、AMM及兑换交易传播如何改变世界

web3-去中心化金融深度剖析:DEX、AMM及兑换交易传播如何改变世界 金融问题 1.个人投资:在不同的时间和可能的情况(状态)下积累财富 2.商业投资:为企业家和企业提供投资生产性活动的资源 目标:跨越时间和…

【笔记】NVIDIA AI Workbench 安装记录

前言 NVIDIA AI Workbench 简介 NVIDIA AI Workbench 是一款由 NVIDIA 推出的集成化开发工具,旨在简化人工智能和机器学习的开发流程。它提供从数据准备到模型训练、优化及部署的全套工具链,支持跨平台协作,适合开发者、数据科学家和企业团队…

积累-Vue.js 开发实用指南:ElementUI 与核心技巧

一、ElementUI 组件高效使用 1. Table 组件展开行优化实现 场景需求:仅在展开行时动态加载数据,避免不必要的接口调用 实现方案: // expand行展开的时候调用expandOpen: async (row, expandedRows) > {// 实时更新展开列const index e…

【BUG】记STM32F030多通道ADC DMA读取乱序问题

STM32F0多通道ADC的校准顺序与DMA乱序问题的本质 声明:本段转载:https://www.cnblogs.com/chihirosan/p/5458673.html 问题描述 通过 uint16_t ConvData[8]保存DMA搬运的ADC转换数值,但是这个数组数值的顺序总是和ADC不是顺序对应的。比如用7…

Java - Mysql数据类型对应

Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…

今天刷SQL

多说几句,JAVA真不行了? 刚刚看到那个tiobe最新的指数,JAVA只剩下8.84%?感觉上次看的时候还有百分之十呢。 185-department-top-three-salaries https://leetcode.com/problems/department-top-three-salaries/description/ 公…