使用时间函数获取数据给字段赋值
root@mysqldb 16:16: [(none)]> select curtime();
+-----------+
| curtime() |
+-----------+
| 16:18:41 |
+-----------+
1 row in set (0.00 sec)
root@mysqldb 16:18: [(none)]> select curdate();
+------------+
| curdate() |
+------------+
| 2023-09-11 |
+------------+
1 row in set (0.00 sec)
root@mysqldb 16:19: [(none)]> select now() ;
+---------------------+
| now() |
+---------------------+
| 2023-09-11 16:19:19 |
+---------------------+
1 row in set (0.00 sec)
root@mysqldb 16:19: [(none)]> select year( curdate() );
+-------------------+
| year( curdate() ) |
+-------------------+
| 2023 |
+-------------------+
1 row in set (0.00 sec)
root@mysqldb 16:19: [(none)]> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2023-09-11 |
+-------------+
1 row in set (0.00 sec)
root@mysqldb 16:20: [(none)]> select day(now());
+------------+
| day(now()) |
+------------+
| 11 |
+------------+
1 row in set (0.00 sec)
-- 当前的时间戳
root@mysqldb 16:31: [d1]> SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
| 1694421194 |
+------------------+
1 row in set (0.00 sec)
-- 将当前时间戳(10位)转换成时间
root@mysqldb 16:35: [d1]> SELECT FROM_UNIXTIME(1694421330);
+---------------------------+
| FROM_UNIXTIME(1694421330) |
+---------------------------+
| 2023-09-11 16:35:30 |
+---------------------------+
1 row in set (0.00 sec)
-- 按照指定格式格式化日期和时间
root@mysqldb 16:36: [d1]> SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');
+-----------------------------------------+
| DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') |
+-----------------------------------------+
| 2023-09-11 16:38:30 |
+-----------------------------------------+
1 row in set (0.00 sec)
root@mysqldb 16:38: [d1]> SELECT DATE_FORMAT(NOW(), '%Y-%m-%d');
+--------------------------------+
| DATE_FORMAT(NOW(), '%Y-%m-%d') |
+--------------------------------+
| 2023-09-11 |
+--------------------------------+
1 row in set (0.00 sec)
root@mysqldb 16:38: [d1]> SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %h:%i:%s %p');
+--------------------------------------------+
| DATE_FORMAT(NOW(), '%Y-%m-%d %h:%i:%s %p') |
+--------------------------------------------+
| 2023-09-11 04:39:17 PM |
+--------------------------------------------+
1 row in set (0.00 sec)
-- 将字符串转换为日期和时间,按照指定的格式。
-- %Y:四位数的年份(例如:2023)
-- %y:两位数的年份(例如:23)
-- %m:两位数的月份(01 到 12)
-- %c:月份(1 到 12)
-- %d:两位数的日期(01 到 31)
-- %e:日期(1 到 31)
-- %H:24 小时制的小时(00 到 23)
-- %h:12 小时制的小时(01 到 12)
-- %i:分钟数(00 到 59)
-- %s:秒数(00 到 59)
-- %p:AM 或 PM
root@mysqldb 16:39: [d1]> SELECT STR_TO_DATE('11/09/2023', '%d/%m/%Y');
+---------------------------------------+
| STR_TO_DATE('11/09/2023', '%d/%m/%Y') |
+---------------------------------------+
| 2023-09-11 |
+---------------------------------------+
1 row in set (0.00 sec)
root@mysqldb 16:44: [d1]> SELECT STR_TO_DATE('11-09-2023 12:34:56 PM', '%d-%m-%Y %h:%i:%s %p');
+---------------------------------------------------------------+
| STR_TO_DATE('11-09-2023 12:34:56 PM', '%d-%m-%Y %h:%i:%s %p') |
+---------------------------------------------------------------+
| 2023-09-11 12:34:56 |
+---------------------------------------------------------------+
1 row in set (0.00 sec)
-- 给指定的日期添加天数或间隔
-- ADDDATE() 和 DATE_ADD() 函数 用于向日期值添加时间间隔,这两个函数的使用方式类似,但是 DATE_ADD() 函数更加通用,因为它可以用于添加不同的时间单位,而 ADDDATE() 函数只能添加日期。
root@mysqldb 16:47: [d1]> SELECT ADDDATE('2023-09-11', INTERVAL 1 DAY);
+---------------------------------------+
| ADDDATE('2023-09-11', INTERVAL 1 DAY) |
+---------------------------------------+
| 2023-09-12 |
+---------------------------------------+
1 row in set (0.00 sec)
-- SUBDATE() 或 DATE_SUB() 函数用于从日期中减去指定的时间间隔
root@mysqldb 16:51: [d1]> SELECT SUBDATE('2023-09-11', INTERVAL 1 DAY);
+---------------------------------------+
| SUBDATE('2023-09-11', INTERVAL 1 DAY) |
+---------------------------------------+
| 2023-09-10 |
+---------------------------------------+
1 row in set (0.00 sec)
root@mysqldb 16:54: [d1]> SELECT SUBDATE('2023-09-11', INTERVAL 1 MONTH);
+-----------------------------------------+
| SUBDATE('2023-09-11', INTERVAL 1 MONTH) |
+-----------------------------------------+
| 2023-08-11 |
+-----------------------------------------+
1 row in set (0.00 sec)
root@mysqldb 16:55: [d1]> SELECT SUBDATE('2023-09-11', INTERVAL 1 YEAR);
+----------------------------------------+
| SUBDATE('2023-09-11', INTERVAL 1 YEAR) |
+----------------------------------------+
| 2022-09-11 |
+----------------------------------------+
1 row in set (0.00 sec)
root@mysqldb 17:00: [d1]> SELECT DAY('2023-09-11');
+-------------------+
| DAY('2023-09-11') |
+-------------------+
| 11 |
+-------------------+
1 row in set (0.00 sec)
root@mysqldb 17:01: [d1]> SELECT MONTH('2023-09-11');
+---------------------+
| MONTH('2023-09-11') |
+---------------------+
| 9 |
+---------------------+
1 row in set (0.00 sec)
root@mysqldb 17:01: [d1]> SELECT YEAR('2023-09-11');
+--------------------+
| YEAR('2023-09-11') |
+--------------------+
| 2023 |
+--------------------+
1 row in set (0.00 sec)
root@mysqldb 17:01: [d1]> SELECT WEEK('2023-09-11');
+--------------------+
| WEEK('2023-09-11') |
+--------------------+
| 37 |
+--------------------+
1 row in set (0.00 sec)
-- WEEKDAY():返回给定日期是星期几,从0(星期一)到6(星期日)
root@mysqldb 17:01: [d1]> SELECT WEEKDAY('2023-09-11');
+-----------------------+
| WEEKDAY('2023-09-11') |
+-----------------------+
| 0 |
+-----------------------+
1 row in set (0.00 sec)
-- DAYOFYEAR():返回给定日期是一年中的第几天。
root@mysqldb 17:04: [d1]> SELECT DAYOFYEAR('2023-09-11');
+-------------------------+
| DAYOFYEAR('2023-09-11') |
+-------------------------+
| 254 |
+-------------------------+
1 row in set (0.00 sec)
root@mysqldb 17:06: [d1]> SELECT HOUR('2023-09-11 17:05:30');
+-----------------------------+
| HOUR('2023-09-11 17:05:30') |
+-----------------------------+
| 17 |
+-----------------------------+
1 row in set (0.00 sec)
root@mysqldb 17:06: [d1]> SELECT HOUR('17:05:30');
+------------------+
| HOUR('17:05:30') |
+------------------+
| 17 |
+------------------+
1 row in set (0.00 sec)
root@mysqldb 17:06: [d1]> SELECT MINUTE('17:05:30');
+--------------------+
| MINUTE('17:05:30') |
+--------------------+
| 5 |
+--------------------+
1 row in set (0.01 sec)
root@mysqldb 17:06: [d1]> SELECT SECOND('17:05:30');
+--------------------+
| SECOND('17:05:30') |
+--------------------+
| 30 |
+--------------------+
1 row in set (0.00 sec)
利用时间函数插入数据
root@mysqldb 16:25: [d1]> INSERT INTO project (task, StartTime)
-> VALUES
-> ("FF", now() );
Query OK, 1 row affected (0.00 sec)
root@mysqldb 16:28: [d1]> SELECT * FROM project;
+------+---------------------+---------------------+
| task | StartTime | endtime |
+------+---------------------+---------------------+
| AA | 2023-07-15 10:00:00 | 2023-08-01 00:00:00 |
| BB | 2023-07-20 10:00:00 | 2023-08-02 10:00:00 |
| CC | 2023-08-16 00:00:00 | 2023-08-25 00:00:00 |
| DD | 2023-09-02 00:00:00 | 2023-09-06 00:00:00 |
| EE | 2023-09-01 10:00:00 | 2023-09-05 00:00:00 |
| FF | 2023-09-11 16:28:08 | NULL |
+------+---------------------+---------------------+
6 rows in set (0.00 sec)
root@mysqldb 16:51: [d1]> UPDATE project
-> SET endtime = DATE_ADD(now(), INTERVAL 1 MONTH)
-> WHERE task ='FF';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
root@mysqldb 16:51: [d1]> SELECT * FROM project;
+------+---------------------+---------------------+
| task | StartTime | endtime |
+------+---------------------+---------------------+
| AA | 2023-07-15 10:00:00 | 2023-08-01 00:00:00 |
| BB | 2023-07-20 10:00:00 | 2023-08-02 10:00:00 |
| CC | 2023-08-16 00:00:00 | 2023-08-25 00:00:00 |
| DD | 2023-09-02 00:00:00 | 2023-09-06 00:00:00 |
| EE | 2023-09-01 10:00:00 | 2023-09-05 00:00:00 |
| FF | 2023-09-11 16:28:08 | 2023-10-11 16:51:29 |
+------+---------------------+---------------------+
6 rows in set (0.00 sec)