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

查看:188
本文介绍了MySQL的日期格式 - 难度插入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图进一步提出一个问题,我昨天问我想知道如何查询不同格式的日期。但现在我正在尝试使用此方法插入(请参阅下面),但是我不能得到它的工作。我已经检查了手册,但它不是初学者友好的!

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


解决方案

将日期放在单引号中并移动圆括号在'yes')之后:

  INSERT INTO custorder 
VALUES('Kevin','yes',STR_TO_DATE('1-01-2012','%d-%m-%Y'));
^ ^
---删除括号 - |并在这里添加------ |






但是您可以使用不含<$ c的日期$ c> STR_TO_DATE()函数,只需使用(Ymd)'20120101''2012-01-01 '格式。检查MySQL文档:日期和时间文字 a>

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


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');

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 ------|


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天全站免登陆