{"id":5260,"date":"2024-11-14T11:54:38","date_gmt":"2024-11-14T03:54:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5260\/"},"modified":"2024-11-14T11:54:38","modified_gmt":"2024-11-14T03:54:38","slug":"mysql%e6%80%8e%e4%b9%88%e8%ae%a1%e7%ae%97%e6%97%b6%e9%97%b4%e5%b7%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5260\/","title":{"rendered":"mysql\u600e\u4e48\u8ba1\u7b97\u65f6\u95f4\u5dee"},"content":{"rendered":"<blockquote><p>\n  \u8ba1\u7b97 mysql \u4e2d\u7684\u65f6\u95f4\u5dee\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u76f4\u63a5\u76f8\u51cf\u3001\u4f7f\u7528 date_sub() \u51fd\u6570\u3001\u4f7f\u7528 timediff() \u51fd\u6570\u548c\u4f7f\u7528 sec_to_time() \u51fd\u6570\u3002\u76f4\u63a5\u76f8\u51cf\u8fd4\u56de\u6beb\u79d2\u5dee\u503c\uff0cdate_sub() \u51fd\u6570\u4ece\u65f6\u95f4\u6233\u4e2d\u51cf\u53bb\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\uff0ctimediff() \u51fd\u6570\u8fd4\u56de\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u5b57\u7b26\u4e32\uff0c\u800c sec_to_time() \u51fd\u6570\u5c06\u6beb\u79d2\u503c\u8f6c\u6362\u4e3a\u53ef\u8bfb\u683c\u5f0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052617514064785.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u8ba1\u7b97\u65f6\u95f4\u5dee\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u8ba1\u7b97\u65f6\u95f4\u5dee\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u76f8\u51cf<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u51cf\u6cd5\u8fd0\u7b97\u7b26 (-) \u6765\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u4e4b\u95f4\u7684\u5dee\u503c\uff0c\u7ed3\u679c\u5c06\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT timestamp2 - timestamp1 AS time_diff;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 DATE_SUB() \u51fd\u6570<\/strong><\/p>\n<p>DATE_SUB() \u51fd\u6570\u5141\u8bb8\u60a8\u6307\u5b9a\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694\uff0c\u5982\u79d2\u3001\u5206\u949f\u6216\u5c0f\u65f6\uff0c\u5e76\u5c06\u6b64\u95f4\u9694\u4ece\u6307\u5b9a\u7684\u65f6\u95f4\u6233\u4e2d\u51cf\u53bb\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT DATE_SUB(timestamp2, INTERVAL 1 HOUR) AS adjusted_timestamp;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 TIMEDIFF() \u51fd\u6570<\/strong><\/p>\n<p>TIMEDIFF() \u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u4e4b\u95f4\u7684\u5dee\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e26\u6709\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT TIMEDIFF(timestamp2, timestamp1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 SEC_TO_TIME() \u51fd\u6570<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u4ee5\u53ef\u8bfb\u7684\u683c\u5f0f\u663e\u793a\u65f6\u95f4\u5dee\uff0c\u53ef\u4ee5\u4f7f\u7528 SEC_TO_TIME() \u51fd\u6570\u5c06\u6beb\u79d2\u503c\u8f6c\u6362\u4e3a\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT SEC_TO_TIME(time_diff) AS time_diff_formatted;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5904\u7406\u7a7a\u503c\u548c\u65e0\u6548\u6570\u636e<\/strong><\/p>\n<p>\u5728\u8ba1\u7b97\u65f6\u95f4\u5dee\u65f6\uff0c\u5904\u7406\u7a7a\u503c\u548c\u65e0\u6548\u6570\u636e\u975e\u5e38\u91cd\u8981\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 ISNULL() \u51fd\u6570\u6765\u68c0\u67e5\u7a7a\u503c\uff0c\u5e76\u4f7f\u7528 COALESCE() \u51fd\u6570\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u4e4b\u95f4\u7684\u5dee\u503c\uff1a<\/p>\n<pre>-- \u76f4\u63a5\u76f8\u51cf\nSELECT timestamp2 - timestamp1 AS time_diff;\n\n-- \u4f7f\u7528 DATE_SUB() \u51fd\u6570\nSELECT DATE_SUB(timestamp2, INTERVAL 1 HOUR) AS adjusted_timestamp;\n\n-- \u4f7f\u7528 TIMEDIFF() \u51fd\u6570\nSELECT TIMEDIFF(timestamp2, timestamp1);\n\n-- \u4f7f\u7528 SEC_TO_TIME() \u51fd\u6570\nSELECT SEC_TO_TIME(time_diff) AS time_diff_formatted;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u8ba1\u7b97\u65f6\u95f4\u5dee\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>\u8ba1\u7b97 mysql \u4e2d\u7684\u65f6\u95f4\u5dee\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u76f4\u63a5\u76f8\u51cf\u3001\u4f7f\u7528 date_sub() \u51fd\u6570\u3001\u4f7f\u7528 timediff() \u51fd\u6570\u548c\u4f7f\u7528 sec_to_time() \u51fd\u6570\u3002\u76f4\u63a5\u76f8\u51cf\u8fd4\u56de\u6beb\u79d2\u5dee\u503c\uff0cdate_sub() \u51fd\u6570\u4ece\u65f6\u95f4\u6233\u4e2d\u51cf\u53bb\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\uff0ctimediff() \u51fd\u6570\u8fd4\u56de\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u5b57\u7b26\u4e32\uff0c\u800c sec_to_time() \u51fd\u6570\u5c06\u6beb\u79d2\u503c\u8f6c\u6362\u4e3a\u53ef\u8bfb\u683c\u5f0f\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u8ba1\u7b97\u65f6\u95f4\u5dee \u76f4\u63a5\u76f8\u51cf \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u51cf\u6cd5\u8fd0\u7b97\u7b26 (-) \u6765\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u4e4b\u95f4\u7684\u5dee\u503c\uff0c\u7ed3\u679c\u5c06\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u3002\u4f8b\u5982\uff1a SELECT timestamp2 &#8211; timestamp1 AS time_diff; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 DATE_SUB() \u51fd\u6570 DATE_SUB() \u51fd\u6570\u5141\u8bb8\u60a8\u6307\u5b9a\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694\uff0c\u5982\u79d2\u3001\u5206\u949f\u6216\u5c0f\u65f6\uff0c\u5e76\u5c06\u6b64\u95f4\u9694\u4ece\u6307\u5b9a\u7684\u65f6\u95f4\u6233\u4e2d\u51cf\u53bb\u3002\u4f8b\u5982\uff1a SELECT DATE_SUB(timestamp2, INTERVAL 1 HOUR) AS adjusted_timestamp; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 TIMEDIFF() \u51fd\u6570 TIMEDIFF() \u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u4e4b\u95f4\u7684\u5dee\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e26\u6709\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a SELECT TIMEDIFF(timestamp2, timestamp1); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 SEC_TO_TIME() \u51fd\u6570 \u5982\u679c\u60a8\u5e0c\u671b\u4ee5\u53ef\u8bfb\u7684\u683c\u5f0f\u663e\u793a\u65f6\u95f4\u5dee\uff0c\u53ef\u4ee5\u4f7f\u7528 SEC_TO_TIME() \u51fd\u6570\u5c06\u6beb\u79d2\u503c\u8f6c\u6362\u4e3a\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u4f8b\u5982\uff1a [&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-5260","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5260","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=5260"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5260\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}