{"id":5151,"date":"2024-11-14T12:37:46","date_gmt":"2024-11-14T04:37:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5151\/"},"modified":"2024-11-14T12:37:46","modified_gmt":"2024-11-14T04:37:46","slug":"mysql%e6%97%b6%e9%97%b4%e6%88%b3%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5151\/","title":{"rendered":"mysql\u65f6\u95f4\u6233\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  mysql \u4f7f\u7528 timestamp \u6570\u636e\u7c7b\u578b\u5b58\u50a8\u65f6\u95f4\u6233\uff0c\u53ef\u81ea\u52a8\u751f\u6210\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa timestamp \u5217\uff1acreate table my_table (event_time timestamp not null);\u63d2\u5165\u65f6\u95f4\u6233\u503c\uff1ainsert into my_table (event_time) values (current_timestamp);\u83b7\u53d6\u65f6\u95f4\u6233\u503c\uff1aselect now();\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/22\/2024052217182414573.jpg\" class=\"aligncenter\" title=\"mysql\u65f6\u95f4\u6233\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"mysql\u65f6\u95f4\u6233\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d\u5982\u4f55\u4f7f\u7528\u65f6\u95f4\u6233<\/strong><\/p>\n<p>MySQL \u4e2d\u7684\u65f6\u95f4\u6233\u662f\u4e00\u79cd\u7279\u6b8a\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u7684\u503c\u3002\u5b83\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u8bb0\u5f55\u548c\u8ddf\u8e2a\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u521b\u5efa\u65f6\u95f4\u6233\u5217<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u65f6\u95f4\u6233\u5217\uff0c\u8bf7\u4f7f\u7528 TIMESTAMP \u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<pre>CREATE TABLE my_table (\n  event_time TIMESTAMP NOT NULL\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8bf7\u6ce8\u610f\uff0cNOT NULL \u7ea6\u675f\u53ef\u786e\u4fdd\u4e3a\u6bcf\u4e00\u884c\u5b58\u50a8\u65f6\u95f4\u6233\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d2\u5165\u65f6\u95f4\u6233\u503c<\/strong><\/p>\n<p>MySQL \u4f1a\u81ea\u52a8\u4e3a\u65b0\u63d2\u5165\u7684\u884c\u751f\u6210\u65f6\u95f4\u6233\u503c\u3002\u4ee5\u4e0b SQL \u8bed\u53e5\u5c06\u63d2\u5165\u5f53\u524d\u65f6\u95f4\u6233\uff1a<\/p>\n<pre>INSERT INTO my_table (event_time) VALUES (CURRENT_TIMESTAMP);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u65f6\u95f4\u6233\u503c<\/strong><\/p>\n<p>\u8981\u83b7\u53d6\u65f6\u95f4\u6233\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 NOW() \u6216 CURRENT_TIMESTAMP \u51fd\u6570\uff1a<\/p>\n<pre>SELECT NOW();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u503c\u3002<\/p>\n<p><strong>\u65f6\u95f4\u6233\u683c\u5f0f<\/strong><\/p>\n<p>MySQL \u4e2d\u7684\u65f6\u95f4\u6233\u4ee5\u4ee5\u4e0b\u683c\u5f0f\u5b58\u50a8\uff1a<\/p>\n<pre>YYYY-MM-DD HH:MM:SS.fffffffff<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>YYYY \u662f\u5e74\u4efd<\/li>\n<li>MM \u662f\u6708\u4efd<\/li>\n<li>DD \u662f\u65e5\u671f<\/li>\n<li>HH \u662f\u5c0f\u65f6\uff0824 \u5c0f\u65f6\u5236\uff09<\/li>\n<li>MM \u662f\u5206\u949f<\/li>\n<li>SS \u662f\u79d2<\/li>\n<li>f \u662f\u5fae\u79d2\uff08\u53ef\u9009\uff09<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u65f6\u95f4\u6233\u7684\u4f18\u52bf<\/strong><\/p>\n<ul>\n<li> <strong>\u7cbe\u786e\u6027\uff1a<\/strong>\u65f6\u95f4\u6233\u63d0\u4f9b\u4e86\u7cbe\u786e\u5230\u5fae\u79d2\u7684\u65f6\u95f4\u8bb0\u5f55\u3002<\/li>\n<li> <strong>\u81ea\u52a8\u751f\u6210\uff1a<\/strong>MySQL \u4f1a\u81ea\u52a8\u751f\u6210\u65f6\u95f4\u6233\u503c\uff0c\u65e0\u9700\u624b\u52a8\u63d2\u5165\u3002<\/li>\n<li> <strong>\u6548\u7387\uff1a<\/strong>\u65f6\u95f4\u6233\u662f\u4e00\u4e2a\u7d27\u51d1\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6709\u52a9\u4e8e\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u65f6\u95f4\u6233\u600e\u4e48\u5199\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 \u4f7f\u7528 timestamp \u6570\u636e\u7c7b\u578b\u5b58\u50a8\u65f6\u95f4\u6233\uff0c\u53ef\u81ea\u52a8\u751f\u6210\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa timestamp \u5217\uff1acreate table my_table (event_time timestamp not null);\u63d2\u5165\u65f6\u95f4\u6233\u503c\uff1ainsert into my_table (event_time) values (current_timestamp);\u83b7\u53d6\u65f6\u95f4\u6233\u503c\uff1aselect now(); MySQL \u4e2d\u5982\u4f55\u4f7f\u7528\u65f6\u95f4\u6233 MySQL \u4e2d\u7684\u65f6\u95f4\u6233\u662f\u4e00\u79cd\u7279\u6b8a\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u7684\u503c\u3002\u5b83\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u8bb0\u5f55\u548c\u8ddf\u8e2a\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u95f4\u3002 \u5982\u4f55\u521b\u5efa\u65f6\u95f4\u6233\u5217 \u8981\u521b\u5efa\u65f6\u95f4\u6233\u5217\uff0c\u8bf7\u4f7f\u7528 TIMESTAMP \u6570\u636e\u7c7b\u578b\uff1a CREATE TABLE my_table ( event_time TIMESTAMP NOT NULL ); \u767b\u5f55\u540e\u590d\u5236 \u8bf7\u6ce8\u610f\uff0cNOT NULL \u7ea6\u675f\u53ef\u786e\u4fdd\u4e3a\u6bcf\u4e00\u884c\u5b58\u50a8\u65f6\u95f4\u6233\u503c\u3002 \u5982\u4f55\u63d2\u5165\u65f6\u95f4\u6233\u503c MySQL \u4f1a\u81ea\u52a8\u4e3a\u65b0\u63d2\u5165\u7684\u884c\u751f\u6210\u65f6\u95f4\u6233\u503c\u3002\u4ee5\u4e0b SQL \u8bed\u53e5\u5c06\u63d2\u5165\u5f53\u524d\u65f6\u95f4\u6233\uff1a INSERT INTO my_table (event_time) VALUES (CURRENT_TIMESTAMP); \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f55\u83b7\u53d6\u65f6\u95f4\u6233\u503c \u8981\u83b7\u53d6\u65f6\u95f4\u6233\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 NOW() \u6216 [&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-5151","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5151","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=5151"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5151\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}