如何用MySQL日期格式在单引号中插入日期?
要以日期格式插入日期,请使用带单引号的带日期的str_to_date()函数。以下是语法-
insert into yourTableName values(Value1,value2,......ValueN,str_to_date(‘anyDate’,’%Y-%m-%d’));
这是MySQL中的日期格式-
让我们首先创建一个表-
mysql> create table BookDateDemo −> ( −> BookName varchar(200), −> BookIssueDate datetime −> );
现在实现我们上面讨论的语法,以MySQL日期格式插入日期。在这里,我们将日期设置为单引号-
mysql> insert into BookDateDemo values('Let us C',str_to_date('2018-11-28','%Y-%m-%d')); mysql> insert into BookDateDemo values('The C++ Programming',str_to_date('2018-11-29','%Y-%m-%d')); mysql> insert into BookDateDemo values('C in Depth',str_to_date('2018-11-20','%Y-%m-%d'));
在select语句的帮助下显示所有记录。显示所有记录的查询如下-
mysql> select *from BookDateDemo;
以下是输出-
+---------------------+---------------------+ | BookName | BookIssueDate | +---------------------+---------------------+ | Let us C | 2018-11-28 00:00:00 | | The C++ Programming | 2018-11-29 00:00:00 | | C in Depth | 2018-11-20 00:00:00 | +---------------------+---------------------+ 3 rows in set (0.00 sec)