{"id":6388,"date":"2024-11-14T11:25:28","date_gmt":"2024-11-14T03:25:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6388\/"},"modified":"2024-11-14T11:25:28","modified_gmt":"2024-11-14T03:25:28","slug":"mysql-datetime%e9%bb%98%e8%ae%a4%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6388\/","title":{"rendered":"mysql datetime\u9ed8\u8ba4\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  mysql datetime \u6570\u636e\u7c7b\u578b\u7684\u9ed8\u8ba4\u957f\u5ea6\u4e3a 19 \u4e2a\u5b57\u7b26\uff0c\u683c\u5f0f\u4e3a yyyy-mm-dd hh:mm:ss\uff0c\u5305\u542b\u5e74\u4efd\u3001\u6708\u4efd\u3001\u65e5\u671f\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u6570\u3002\u5bf9\u4e8e\u66f4\u7cbe\u7ec6\u7684\u65f6\u95f4\u4fe1\u606f\uff0c\u5982\u6beb\u79d2\u6216\u5fae\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528 datetime(n) \u6570\u636e\u7c7b\u578b\uff0c\u5176\u4e2d n \u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u4f8b\u5982\uff0cdatetime(3) \u5b58\u50a8\u5230\u6beb\u79d2\uff0cdatetime(6) \u5b58\u50a8\u5230\u5fae\u79d2\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/01\/2024080106311035827.jpg\" class=\"aligncenter\" title=\"mysql datetime\u9ed8\u8ba4\u957f\u5ea6\u63d2\u56fe\" alt=\"mysql datetime\u9ed8\u8ba4\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL DATETIME \u9ed8\u8ba4\u957f\u5ea6<\/strong><\/p>\n<p>MySQL \u4e2d\u7684 DATETIME \u6570\u636e\u7c7b\u578b\u7528\u4e8e\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u4fe1\u606f\u3002\u5b83\u7684\u9ed8\u8ba4\u957f\u5ea6\u4e3a <strong>19<\/strong> \u4e2a\u5b57\u7b26\uff0c\u683c\u5f0f\u4e3a YYYY-MM-DD HH:MM:SS\u3002<\/p>\n<p><strong>\u9ed8\u8ba4\u957f\u5ea6\u542b\u4e49<\/strong><\/p>\n<ul>\n<li> <strong>YYYY\uff1a<\/strong> \u56db\u4f4d\u6570\u8868\u793a\u7684\u5e74\u4efd<\/li>\n<li> <strong>MM\uff1a<\/strong>\u4e24\u4f4d\u6570\u8868\u793a\u7684\u6708\u4efd<\/li>\n<li> <strong>DD\uff1a<\/strong>\u4e24\u4f4d\u6570\u8868\u793a\u7684\u65e5\u671f<\/li>\n<li> <strong>HH\uff1a<\/strong>\u4e24\u4f4d\u6570\u8868\u793a\u7684\u5c0f\u65f6\uff0824 \u5c0f\u65f6\u5236\uff09<\/li>\n<li> <strong>MM\uff1a<\/strong>\u4e24\u4f4d\u6570\u8868\u793a\u7684\u5206\u949f<\/li>\n<li> <strong>SS\uff1a<\/strong>\u4e24\u4f4d\u6570\u8868\u793a\u7684\u79d2\u6570<\/li>\n<\/ul>\n<p><strong>\u957f\u5ea6\u4e0d\u591f\u7528\u600e\u4e48\u529e\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u5b58\u50a8\u66f4\u7cbe\u7ec6\u7684\u65f6\u95f4\u4fe1\u606f\uff0c\u4f8b\u5982\u6beb\u79d2\u6216\u5fae\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528 DATETIME(n) \u6570\u636e\u7c7b\u578b\uff0c\u5176\u4e2d n \u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>DATETIME(3)\uff1a\u5b58\u50a8\u5230\u6beb\u79d2<\/li>\n<li>DATETIME(6)\uff1a\u5b58\u50a8\u5230\u5fae\u79d2<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a event_time \u7684 DATETIME \u5217\uff1a<\/p>\n<pre>CREATE TABLE events (\n  id INT NOT NULL PRIMARY KEY,\n  event_time DATETIME NOT NULL\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63d2\u5165\u4e00\u6761\u5177\u6709\u9ed8\u8ba4\u957f\u5ea6 DATETIME \u503c\u7684\u8bb0\u5f55\uff1a<\/p>\n<pre>INSERT INTO events (id, event_time) VALUES (1, '2023-01-01 00:00:00');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u67e5\u8be2\u5e76\u9a8c\u8bc1\u957f\u5ea6\uff1a<\/p>\n<pre>SELECT LENGTH(event_time) FROM events WHERE id = 1;\n\n-- \u8f93\u51fa\uff1a19<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f datetime\u9ed8\u8ba4\u957f\u5ea6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql datetime \u6570\u636e\u7c7b\u578b\u7684\u9ed8\u8ba4\u957f\u5ea6\u4e3a 19 \u4e2a\u5b57\u7b26\uff0c\u683c\u5f0f\u4e3a yyyy-mm-dd hh:mm:ss\uff0c\u5305\u542b\u5e74\u4efd\u3001\u6708\u4efd\u3001\u65e5\u671f\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u6570\u3002\u5bf9\u4e8e\u66f4\u7cbe\u7ec6\u7684\u65f6\u95f4\u4fe1\u606f\uff0c\u5982\u6beb\u79d2\u6216\u5fae\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528 datetime(n) \u6570\u636e\u7c7b\u578b\uff0c\u5176\u4e2d n \u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u4f8b\u5982\uff0cdatetime(3) \u5b58\u50a8\u5230\u6beb\u79d2\uff0cdatetime(6) \u5b58\u50a8\u5230\u5fae\u79d2\u3002 MySQL DATETIME \u9ed8\u8ba4\u957f\u5ea6 MySQL \u4e2d\u7684 DATETIME \u6570\u636e\u7c7b\u578b\u7528\u4e8e\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u4fe1\u606f\u3002\u5b83\u7684\u9ed8\u8ba4\u957f\u5ea6\u4e3a 19 \u4e2a\u5b57\u7b26\uff0c\u683c\u5f0f\u4e3a YYYY-MM-DD HH:MM:SS\u3002 \u9ed8\u8ba4\u957f\u5ea6\u542b\u4e49 YYYY\uff1a \u56db\u4f4d\u6570\u8868\u793a\u7684\u5e74\u4efd MM\uff1a\u4e24\u4f4d\u6570\u8868\u793a\u7684\u6708\u4efd DD\uff1a\u4e24\u4f4d\u6570\u8868\u793a\u7684\u65e5\u671f HH\uff1a\u4e24\u4f4d\u6570\u8868\u793a\u7684\u5c0f\u65f6\uff0824 \u5c0f\u65f6\u5236\uff09 MM\uff1a\u4e24\u4f4d\u6570\u8868\u793a\u7684\u5206\u949f SS\uff1a\u4e24\u4f4d\u6570\u8868\u793a\u7684\u79d2\u6570 \u957f\u5ea6\u4e0d\u591f\u7528\u600e\u4e48\u529e\uff1f \u5982\u679c\u9700\u8981\u5b58\u50a8\u66f4\u7cbe\u7ec6\u7684\u65f6\u95f4\u4fe1\u606f\uff0c\u4f8b\u5982\u6beb\u79d2\u6216\u5fae\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528 DATETIME(n) \u6570\u636e\u7c7b\u578b\uff0c\u5176\u4e2d n \u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u4f8b\u5982\uff1a DATETIME(3)\uff1a\u5b58\u50a8\u5230\u6beb\u79d2 DATETIME(6)\uff1a\u5b58\u50a8\u5230\u5fae\u79d2 \u793a\u4f8b \u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a event_time \u7684 DATETIME \u5217\uff1a CREATE TABLE events ( id INT NOT NULL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6388","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6388"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6388\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}