在MySQL数据库中储存日期的最简单方法是什么?
要在MySQL中存储日期,使用STR_TO_DATE()方法 -
insert into yourTableName values(STR_TO_DATE('yourDate', '%d/%m/%Y'));
让我们先创建一个表 -
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.62 sec)
使用insert命令在表中插入记录 -
mysql> insert into DemoTable values(STR_TO_DATE('10/01/2013', '%d/%m/%Y')); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(STR_TO_DATE('31/01/2015', '%d/%m/%Y')); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(STR_TO_DATE('23/04/2019', '%d/%m/%Y')); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(STR_TO_DATE('01/03/2019', '%d/%m/%Y')); Query OK, 1 row affected (0.48 sec)
使用select命令从表中显示记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+---------------+ | AdmissionDate | +---------------+ | 2013-01-10 | | 2015-01-31 | | 2019-04-23 | | 2019-03-01 | +---------------+ 4 rows in set (0.00 sec)
广告