MySQL 日期格式 - 插入日期的困难

2022-01-09 00:00:00 format date insert mysql

我想进一步回答我昨天问的一个问题,我想知道如何以不同的格式查询日期.但是现在我正在尝试使用这种方法进行插入(见下文),但是我无法让它工作.我已经检查了手册,但它对初学者不友好!

I am trying to further a question I asked yesterday where I wanted to know how to query a date in a different format. But now I am trying to do an insert using this method (see below) however I can't get it to work. I have checked the manual but it is not beginner friendly!

INSERT INTO custorder VALUES ('Kevin','yes'), STR_TO_DATE('1-01-2012', '%d-%m-%Y');

推荐答案

将日期放在单引号中,并将括号('yes'之后)移到末尾:

Put the date in single quotes and move the parenthesis (after the 'yes') to the end:

INSERT INTO custorder 
  VALUES ('Kevin', 'yes' , STR_TO_DATE('1-01-2012', '%d-%m-%Y') ) ;
                        ^                                     ^
---parenthesis removed--|                and added here ------|

<小时>

但是你总是可以使用没有 STR_TO_DATE() 函数的日期,只需使用 (Ymd) '20120101''2012-01-01'格式.检查 MySQL 文档:日期和时间文字


But you can always use dates without STR_TO_DATE() function, just use the (Y-m-d) '20120101' or '2012-01-01' format. Check the MySQL docs: Date and Time Literals

INSERT INTO custorder 
  VALUES ('Kevin', 'yes', '2012-01-01') ;

相关文章