MySQL日期格式 - 难以插入日期

时间:2012-03-28 12:11:03

标签: mysql date insert format

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

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

5 个答案:

答案 0 :(得分:103)

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

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

但您可以始终使用没有STR_TO_DATE()功能的日期,只需使用(Y-m-d)'20120101''2012-01-01'格式。检查MySQL文档:Date and Time Literals

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

答案 1 :(得分:10)

看起来你没有正确封装你的字符串。试试这个:

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

或者,您可以执行以下操作:

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

但是,我不确定上述SQL是否有效,您可能希望将日期部分移动到括号中。如果您能提供正确的错误,我可以更直接地帮助解决问题。

答案 2 :(得分:6)

mysql插入查询的日期格式为<lst name="responseHeader"><int name="status">500</int><int name="QTime">1</int></lst><lst name="initFailures"/><lst name="error"><lst name="metadata"><str name="error-class">org.apache.solr.common.SolrException</str><str name="root-error-class">java.nio.file.NoSuchFileException</str></lst><str name="msg">Error handling 'status' action </str><str name="trace">org.apache.solr.common.SolrException: Error handling 'status' action at org.apache.solr.handler.admin.CoreAdminOperation$4.call(CoreAdminOperation.java:192) at org.apache.solr.handler.admin.CoreAdminHandler$CallInfo.call(CoreAdminHandler.java:354) at org.apache.solr.handler.admin.CoreAdminHandler.handleRequestBody(CoreAdminHandler.java:153) at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:155) at org.apache.solr.servlet.HttpSolrCall.handleAdminRequest(HttpSolrCall.java:676) at org.apache.solr.servlet.HttpSolrCall.call(HttpSolrCall.java:439) at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:225) at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:183) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handle(Server.java:499) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) at java.lang.Thread.run(Thread.java:745) Caused by: java.nio.file.NoSuchFileException: /var/solr/data/sitecore_master_index/data/index/segments_90 at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55) at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:144) at sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:99) at java.nio.file.Files.readAttributes(Files.java:1737) at java.nio.file.Files.size(Files.java:2332) at org.apache.lucene.store.FSDirectory.fileLength(FSDirectory.java:210) at org.apache.lucene.store.NRTCachingDirectory.fileLength(NRTCachingDirectory.java:127) at org.apache.solr.handler.admin.LukeRequestHandler.getIndexInfo(LukeRequestHandler.java:592) at org.apache.solr.handler.admin.CoreAdminOperation.getCoreStatus(CoreAdminOperation.java:886) at org.apache.solr.handler.admin.CoreAdminOperation$4.call(CoreAdminOperation.java:188) ... 27 more

示例:

YYYY-MM-DD

答案 3 :(得分:1)

自从我遇到这个问题以来,以前答案的附加内容:

如果您确实要在 DATE 列中添加24-May-2005之类的内容,可以执行以下操作:

INSERT INTO someTable(Empid,Date_Joined)
VALUES
    ('S710',STR_TO_DATE('24-May-2005', '%d-%M-%Y'));

在上述查询中请注意,如果它是May(即:字母中的月份),则格式应为%M

注意:我尝试使用最新的MySQL 8.0版,它可以正常工作!

答案 4 :(得分:0)

在PHP中使用包含日期的字符串类型变量时,该变量必须用单引号引起来:

const userList = {
  "8micxp7q5w5tfsrkkw9iys7l2ead7tls": {
    "ident": "8micxp7q5w5tfsrkkw9iys7l2ead7tls",
    "username": "Antidote",
    "posx": 1002,
    "posy": 125,
    "lineWidth": "1",
    "lineColor": "00baff",
    "ignore": false,
    "status": "w"
  },
  "1fjw6u6ok8q6jrqcnr47yymbaida2iwp": {
    "ident": "1fjw6u6ok8q6jrqcnr47yymbaida2iwp",
    "username": "from Lehi, United States",
    "posx": 989,
    "posy": 124,
    "lineWidth": "1",
    "lineColor": "000000",
    "ignore": false,
    "status": "w"
  }
}


console.log(userList['8micxp7q5w5tfsrkkw9iys7l2ead7tls'].username);
/** output: Antidote **/

console.log(userList['1fjw6u6ok8q6jrqcnr47yymbaida2iwp'].username);
/** output: from Lehi, United States **/