{"id":5216,"date":"2024-11-14T12:47:59","date_gmt":"2024-11-14T04:47:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5216\/"},"modified":"2024-11-14T12:47:59","modified_gmt":"2024-11-14T04:47:59","slug":"sql%e7%9a%84%e6%97%b6%e9%97%b4%e6%88%b3%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5216\/","title":{"rendered":"sql\u7684\u65f6\u95f4\u6233\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql\u65f6\u95f4\u6233\u7c7b\u578b\u7528\u4e8e\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u53ef\u4f7f\u7528 timestamp \u6216 datetime \u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u3002\u60a8\u53ef\u4ee5\u63d0\u53d6\u65f6\u95f4\u7ec4\u4ef6\uff08\u5e74\u4efd\u3001\u6708\u4efd\u7b49\uff09\uff0c\u6bd4\u8f83\u65f6\u95f4\u6233\uff0c\u5e76\u4f7f\u7528 current_timestamp \u66f4\u65b0\u65f6\u95f4\u6233\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052421272351902.jpg\" class=\"aligncenter\" title=\"sql\u7684\u65f6\u95f4\u6233\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u7684\u65f6\u95f4\u6233\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u7684\u65f6\u95f4\u6233\u7c7b\u578b<\/strong><\/p>\n<p>SQL \u65f6\u95f4\u6233\u7c7b\u578b\u7528\u4e8e\u5b58\u50a8\u548c\u8868\u793a\u65e5\u671f\u548c\u65f6\u95f4\u503c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u8bb0\u5f55\u4e8b\u4ef6\u53d1\u751f\u6216\u6570\u636e\u66f4\u65b0\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528 TIMESTAMP \u6216 DATETIME \u6570\u636e\u7c7b\u578b\u6765\u5b9a\u4e49\u4e00\u4e2a\u65f6\u95f4\u6233\u5217\u3002TIMESTAMP \u4ec5\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u800c DATETIME \u8fd8\u5305\u62ec\u79d2\u4ee5\u4e0b\u7684\u5c0f\u6570\u90e8\u5206\u3002<\/p>\n<pre>CREATE TABLE events (\n  id INT NOT NULL,\n  event_time TIMESTAMP NOT NULL\n);\nINSERT INTO events (event_time) VALUES (CURRENT_TIMESTAMP);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u63d0\u53d6\u65f6\u95f4\u7ec4\u4ef6<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 SQL \u51fd\u6570\u63d0\u53d6\u65f6\u95f4\u6233\u7684\u5404\u4e2a\u7ec4\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>YEAR(timestamp)\uff1a\u8fd4\u56de\u5e74\u4efd<\/li>\n<li>MONTH(timestamp)\uff1a\u8fd4\u56de\u6708\u4efd<\/li>\n<li>DAY(timestamp)\uff1a\u8fd4\u56de\u65e5\u671f<\/li>\n<li>HOUR(timestamp)\uff1a\u8fd4\u56de\u5c0f\u65f6<\/li>\n<li>MINUTE(timestamp)\uff1a\u8fd4\u56de\u5206\u949f<\/li>\n<li>SECOND(timestamp)\uff1a\u8fd4\u56de\u79d2<\/li>\n<\/ul>\n<pre>SELECT\n  YEAR(event_time),\n  MONTH(event_time),\n  DAY(event_time),\n  HOUR(event_time),\n  MINUTE(event_time),\n  SECOND(event_time)\nFROM events;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6bd4\u8f83\u65f6\u95f4\u6233<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u6765\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u6233\uff1a<\/p>\n<ul>\n<li>=\uff1a\u76f8\u7b49<\/li>\n<li>!=\uff1a\u4e0d\u76f8\u7b49<\/li>\n<li>&gt;\uff1a\u5927\u4e8e<\/li>\n<li> <\/li>\n<li>&gt;=\uff1a\u5927\u4e8e\u7b49\u4e8e<\/li>\n<li> <\/li>\n<\/ul>\n<pre>SELECT\n  *\nFROM events\nWHERE event_time &gt;= '2023-01-01' AND event_time &lt;= '2023-01-31';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u66f4\u65b0\u65f6\u95f4\u6233<\/strong><\/p>\n<p>\u8981\u66f4\u65b0\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u4f7f\u7528 CURRENT_TIMESTAMP \u5173\u952e\u5b57\u6216 NOW() \u51fd\u6570\uff0c\u5b83\u4eec\u90fd\u4f1a\u8fd4\u56de\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff1a<\/p>\n<pre>UPDATE events\nSET event_time = CURRENT_TIMESTAMP\nWHERE id = 1;\n\n-- \u6216\n\nUPDATE events\nSET event_time = NOW()\nWHERE id = 1;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u7684\u65f6\u95f4\u6233\u600e\u4e48\u7528\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>sql\u65f6\u95f4\u6233\u7c7b\u578b\u7528\u4e8e\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u53ef\u4f7f\u7528 timestamp \u6216 datetime \u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u3002\u60a8\u53ef\u4ee5\u63d0\u53d6\u65f6\u95f4\u7ec4\u4ef6\uff08\u5e74\u4efd\u3001\u6708\u4efd\u7b49\uff09\uff0c\u6bd4\u8f83\u65f6\u95f4\u6233\uff0c\u5e76\u4f7f\u7528 current_timestamp \u66f4\u65b0\u65f6\u95f4\u6233\u3002 SQL \u4e2d\u7684\u65f6\u95f4\u6233\u7c7b\u578b SQL \u65f6\u95f4\u6233\u7c7b\u578b\u7528\u4e8e\u5b58\u50a8\u548c\u8868\u793a\u65e5\u671f\u548c\u65f6\u95f4\u503c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u8bb0\u5f55\u4e8b\u4ef6\u53d1\u751f\u6216\u6570\u636e\u66f4\u65b0\u7684\u65f6\u95f4\u3002 \u7528\u6cd5 \u4f7f\u7528 TIMESTAMP \u6216 DATETIME \u6570\u636e\u7c7b\u578b\u6765\u5b9a\u4e49\u4e00\u4e2a\u65f6\u95f4\u6233\u5217\u3002TIMESTAMP \u4ec5\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u800c DATETIME \u8fd8\u5305\u62ec\u79d2\u4ee5\u4e0b\u7684\u5c0f\u6570\u90e8\u5206\u3002 CREATE TABLE events ( id INT NOT NULL, event_time TIMESTAMP NOT NULL ); INSERT INTO events (event_time) VALUES (CURRENT_TIMESTAMP); \u767b\u5f55\u540e\u590d\u5236 \u63d0\u53d6\u65f6\u95f4\u7ec4\u4ef6 \u60a8\u53ef\u4ee5\u4f7f\u7528 SQL \u51fd\u6570\u63d0\u53d6\u65f6\u95f4\u6233\u7684\u5404\u4e2a\u7ec4\u4ef6\uff0c\u4f8b\u5982\uff1a YEAR(timestamp)\uff1a\u8fd4\u56de\u5e74\u4efd MONTH(timestamp)\uff1a\u8fd4\u56de\u6708\u4efd DAY(timestamp)\uff1a\u8fd4\u56de\u65e5\u671f HOUR(timestamp)\uff1a\u8fd4\u56de\u5c0f\u65f6 MINUTE(timestamp)\uff1a\u8fd4\u56de\u5206\u949f SECOND(timestamp)\uff1a\u8fd4\u56de\u79d2 SELECT YEAR(event_time), MONTH(event_time), DAY(event_time), HOUR(event_time), [&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-5216","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5216","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=5216"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5216\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}