{"id":5177,"date":"2024-11-14T11:11:24","date_gmt":"2024-11-14T03:11:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5177\/"},"modified":"2024-11-14T11:11:24","modified_gmt":"2024-11-14T03:11:24","slug":"%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%addatediff%e5%87%bd%e6%95%b0%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5177\/","title":{"rendered":"\u6570\u636e\u5e93\u4e2ddatediff\u51fd\u6570\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  datediff() \u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u8bed\u6cd5\u4e3a datediff(interval, start_date, end_date)\u3002\u53c2\u6570 interval \u6307\u5b9a\u5dee\u5f02\u7c7b\u578b (&#8220;yy&#8221; \u5e74\u3001&#8221;mm&#8221; \u6708\u7b49)\uff0cstart_date \u548c end_date \u5206\u522b\u6307\u5b9a\u5dee\u5f02\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u65e5\u671f\u3002\u6b64\u51fd\u6570\u4e0d\u8003\u8651\u95f0\u5e74\u6216\u65f6\u533a\u5dee\u5f02\uff0c\u5e76\u4e14\u5904\u7406\u7a7a\u503c\u4f1a\u8fd4\u56de null\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052407453498950.jpg\" class=\"aligncenter\" title=\"\u6570\u636e\u5e93\u4e2ddatediff\u51fd\u6570\u7528\u6cd5\u63d2\u56fe\" alt=\"\u6570\u636e\u5e93\u4e2ddatediff\u51fd\u6570\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>Datediff() \u51fd\u6570\u7528\u6cd5<\/strong><\/p>\n<p>Datediff() \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\u5f02\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>DATEDIFF(interval, start_date, end_date)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>interval<\/strong>\uff1a\u6307\u5b9a\u8981\u8ba1\u7b97\u7684\u5dee\u5f02\u7c7b\u578b\uff0c\u53ef\u4ee5\u662f &#8220;yy&#8221;\uff08\u5e74\uff09\u3001&#8221;mm&#8221;\uff08\u6708\uff09\u3001&#8221;dd&#8221;\uff08\u5929\uff09\u3001&#8221;hh&#8221;\uff08\u5c0f\u65f6\uff09\u3001&#8221;mi&#8221;\uff08\u5206\u949f\uff09\u6216 &#8220;ss&#8221;\uff08\u79d2\uff09\u3002<\/li>\n<li> <strong>start_date<\/strong>\uff1a\u8ba1\u7b97\u5f00\u59cb\u7684\u65e5\u671f\u3002<\/li>\n<li> <strong>end_date<\/strong>\uff1a\u8ba1\u7b97\u7ed3\u675f\u7684\u65e5\u671f\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5\u793a\u4f8b<\/strong><\/p>\n<p><strong>\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\u5f02\uff1a<\/strong><\/p>\n<pre>SELECT DATEDIFF(dd, '2023-03-08', '2023-03-15') AS days_diff;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u4efd\u5dee\u5f02\uff1a<\/strong><\/p>\n<pre>SELECT DATEDIFF(mm, '2022-01-01', '2023-03-01') AS months_diff;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5e74\u6570\u5dee\u5f02\uff1a<\/strong><\/p>\n<pre>SELECT DATEDIFF(yy, '1990-01-01', '2023-03-01') AS years_diff;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5904\u7406\u7a7a\u503c\uff1a<\/strong><\/p>\n<p>\u5982\u679c <strong>start_date<\/strong> \u6216 <strong>end_date<\/strong> \u4e3a\u7a7a\uff0cDatediff() \u51fd\u6570\u5c06\u8fd4\u56de <strong>NULL<\/strong>\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>Datediff() \u51fd\u6570\u4e0d\u8003\u8651\u95f0\u5e74\u6216\u65f6\u533a\u5dee\u5f02\u3002<\/li>\n<li>\u5bf9\u4e8e\u8ba1\u7b97\u65e5\u671f\u8303\u56f4\u5185\u7684\u5dee\u5f02\uff0c\u8bf7\u4f7f\u7528 BETWEEN \u5173\u952e\u5b57\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>SELECT DATEDIFF(dd, start_date, end_date)\nFROM table_name\nWHERE start_date BETWEEN '2023-01-01' AND '2023-12-31';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u5e93\u4e2ddatediff\u51fd\u6570\u7528\u6cd5\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>datediff() \u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u8bed\u6cd5\u4e3a datediff(interval, start_date, end_date)\u3002\u53c2\u6570 interval \u6307\u5b9a\u5dee\u5f02\u7c7b\u578b (&#8220;yy&#8221; \u5e74\u3001&#8221;mm&#8221; \u6708\u7b49)\uff0cstart_date \u548c end_date \u5206\u522b\u6307\u5b9a\u5dee\u5f02\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u65e5\u671f\u3002\u6b64\u51fd\u6570\u4e0d\u8003\u8651\u95f0\u5e74\u6216\u65f6\u533a\u5dee\u5f02\uff0c\u5e76\u4e14\u5904\u7406\u7a7a\u503c\u4f1a\u8fd4\u56de null\u3002 Datediff() \u51fd\u6570\u7528\u6cd5 Datediff() \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\u5f02\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a DATEDIFF(interval, start_date, end_date) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a interval\uff1a\u6307\u5b9a\u8981\u8ba1\u7b97\u7684\u5dee\u5f02\u7c7b\u578b\uff0c\u53ef\u4ee5\u662f &#8220;yy&#8221;\uff08\u5e74\uff09\u3001&#8221;mm&#8221;\uff08\u6708\uff09\u3001&#8221;dd&#8221;\uff08\u5929\uff09\u3001&#8221;hh&#8221;\uff08\u5c0f\u65f6\uff09\u3001&#8221;mi&#8221;\uff08\u5206\u949f\uff09\u6216 &#8220;ss&#8221;\uff08\u79d2\uff09\u3002 start_date\uff1a\u8ba1\u7b97\u5f00\u59cb\u7684\u65e5\u671f\u3002 end_date\uff1a\u8ba1\u7b97\u7ed3\u675f\u7684\u65e5\u671f\u3002 \u7528\u6cd5\u793a\u4f8b \u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\u5f02\uff1a SELECT DATEDIFF(dd, &#8216;2023-03-08&#8217;, &#8216;2023-03-15&#8217;) AS days_diff; \u767b\u5f55\u540e\u590d\u5236 \u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u4efd\u5dee\u5f02\uff1a SELECT DATEDIFF(mm, &#8216;2022-01-01&#8217;, &#8216;2023-03-01&#8217;) AS months_diff; \u767b\u5f55\u540e\u590d\u5236 \u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5e74\u6570\u5dee\u5f02\uff1a SELECT DATEDIFF(yy, &#8216;1990-01-01&#8217;, &#8216;2023-03-01&#8217;) AS years_diff; \u767b\u5f55\u540e\u590d\u5236 \u5904\u7406\u7a7a\u503c\uff1a \u5982\u679c [&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-5177","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5177","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=5177"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5177\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}