{"id":5176,"date":"2024-11-14T11:11:11","date_gmt":"2024-11-14T03:11:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5176\/"},"modified":"2024-11-14T11:11:11","modified_gmt":"2024-11-14T03:11:11","slug":"datediff%e5%87%bd%e6%95%b0%e5%9c%a8mysql%e4%b8%ad%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5176\/","title":{"rendered":"datediff\u51fd\u6570\u5728MySQL\u4e2d\u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e2d\u7684 datediff \u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\uff0c\u8bed\u6cd5\u4e3a\uff1adatediff(end_date, start_date)\u3002\u5b83\u53ea\u8003\u8651\u65e5\u671f\uff0c\u4e0d\u53d7\u65f6\u95f4\u90e8\u5206\u7684\u5f71\u54cd\u3002\u51fd\u6570\u8fd4\u56de\u4ee5\u5929\u4e3a\u5355\u4f4d\u7684\u65e5\u671f\u5dee\uff0c\u5982\u679c end_date \u5c0f\u4e8e start_date\uff0c\u5219\u8fd4\u56de\u8d1f\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052407422554606.jpg\" class=\"aligncenter\" title=\"datediff\u51fd\u6570\u5728MySQL\u4e2d\u7684\u7528\u6cd5\u63d2\u56fe\" alt=\"datediff\u51fd\u6570\u5728MySQL\u4e2d\u7684\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>datediff \u51fd\u6570\u5728 MySQL \u4e2d\u7684\u7528\u6cd5<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>datediff \u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>DATEDIFF(end_date, start_date)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<ul>\n<li> <strong>end_date:<\/strong> \u7ed3\u675f\u65e5\u671f\u3002<\/li>\n<li> <strong>start_date:<\/strong> \u5f00\u59cb\u65e5\u671f\u3002<\/li>\n<\/ul>\n<p><strong>\u8fd4\u56de\u503c<\/strong><br \/>\u4ee5\u5929\u4e3a\u5355\u4f4d\u7684\u65e5\u671f\u5dee\u3002\u5982\u679c end_date \u5c0f\u4e8e start_date\uff0c\u5219\u8fd4\u56de\u8d1f\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u8981\u8ba1\u7b97 2023 \u5e74 5 \u6708 1 \u65e5\u548c 2023 \u5e74 6 \u6708 1 \u65e5\u4e4b\u95f4\u7684\u5929\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT DATEDIFF('2023-06-01', '2023-05-01');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u7ed3\u679c\uff1a<strong>31<\/strong>\uff0c\u8868\u793a\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u6709 31 \u5929\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>datediff \u51fd\u6570\u4e0d\u53d7\u65f6\u95f4\u90e8\u5206\u7684\u5f71\u54cd\uff0c\u53ea\u8003\u8651\u65e5\u671f\u3002<\/li>\n<li>\u5982\u679c start_date \u6216 end_date \u4e0d\u662f\u6709\u6548\u7684\u65e5\u671f\uff0c\u5219\u51fd\u6570\u8fd4\u56de NULL\u3002<\/li>\n<li>\u5982\u679c end_date \u5c0f\u4e8e start_date\uff0c\u5219\u8fd4\u56de\u8d1f\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>datediff \u51fd\u6570\u53ef\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5e74\u9f84\u3001\u5de5\u4f5c\u7ecf\u9a8c\u6216\u8ba2\u5355\u5ef6\u8fdf\u65f6\u95f4\u3002<\/li>\n<li>\u786e\u5b9a\u4e24\u4e2a\u4e8b\u4ef6\u4e4b\u95f4\u7684\u6301\u7eed\u65f6\u95f4\u3002<\/li>\n<li>\u68c0\u67e5\u65e5\u671f\u8303\u56f4\u7684\u6709\u6548\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fdatediff\u51fd\u6570\u5728MySQL\u4e2d\u7684\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>mysql \u4e2d\u7684 datediff \u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\uff0c\u8bed\u6cd5\u4e3a\uff1adatediff(end_date, start_date)\u3002\u5b83\u53ea\u8003\u8651\u65e5\u671f\uff0c\u4e0d\u53d7\u65f6\u95f4\u90e8\u5206\u7684\u5f71\u54cd\u3002\u51fd\u6570\u8fd4\u56de\u4ee5\u5929\u4e3a\u5355\u4f4d\u7684\u65e5\u671f\u5dee\uff0c\u5982\u679c end_date \u5c0f\u4e8e start_date\uff0c\u5219\u8fd4\u56de\u8d1f\u503c\u3002 datediff \u51fd\u6570\u5728 MySQL \u4e2d\u7684\u7528\u6cd5 \u7b80\u4ecbdatediff \u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\u3002 \u8bed\u6cd5 DATEDIFF(end_date, start_date) \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570 end_date: \u7ed3\u675f\u65e5\u671f\u3002 start_date: \u5f00\u59cb\u65e5\u671f\u3002 \u8fd4\u56de\u503c\u4ee5\u5929\u4e3a\u5355\u4f4d\u7684\u65e5\u671f\u5dee\u3002\u5982\u679c end_date \u5c0f\u4e8e start_date\uff0c\u5219\u8fd4\u56de\u8d1f\u503c\u3002 \u793a\u4f8b \u8981\u8ba1\u7b97 2023 \u5e74 5 \u6708 1 \u65e5\u548c 2023 \u5e74 6 \u6708 1 \u65e5\u4e4b\u95f4\u7684\u5929\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT DATEDIFF(&#8216;2023-06-01&#8217;, &#8216;2023-05-01&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u8fd4\u56de\u7ed3\u679c\uff1a31\uff0c\u8868\u793a\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u6709 31 \u5929\u3002 \u6ce8\u610f\u4e8b\u9879 datediff \u51fd\u6570\u4e0d\u53d7\u65f6\u95f4\u90e8\u5206\u7684\u5f71\u54cd\uff0c\u53ea\u8003\u8651\u65e5\u671f\u3002 \u5982\u679c start_date \u6216 end_date [&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-5176","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5176","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=5176"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5176\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}