MySQL 日期格式 - 插入日期的困难 [英] MySQL date formats - difficulty Inserting a date

查看:42
本文介绍了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') ;

这篇关于MySQL 日期格式 - 插入日期的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆