{"id":76456,"date":"2024-10-16T06:49:15","date_gmt":"2024-10-15T22:49:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76456\/"},"modified":"2024-10-16T06:49:15","modified_gmt":"2024-10-15T22:49:15","slug":"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%97%a5%e6%9c%9f%e5%87%bd%e6%95%b8%e8%88%87%e6%99%82%e9%96%93%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76456\/","title":{"rendered":"MySQL\u6578\u64da\u5eab\u7684\u65e5\u671f\u51fd\u6578\u8207\u6642\u9593\u51fd\u6578"},"content":{"rendered":"<h1 id=\"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%97%a5%e6%9c%9f%e5%87%bd%e6%95%b8%e8%88%87%e6%99%82%e9%96%93%e5%87%bd%e6%95%b8-PvAgajwBTd\">MySQL\u6578\u64da\u5eab\u7684\u65e5\u671f\u51fd\u6578\u8207\u6642\u9593\u51fd\u6578<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0c\u65e5\u671f\u548c\u6642\u9593\u7684\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u9032\u884c\u6578\u64da\u5206\u6790\u548c\u5831\u544a\u7684\u60c5\u6cc1\u4e0b\u3002MySQL\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65e5\u671f\u548c\u6642\u9593\u51fd\u6578\uff0c\u5e6b\u52a9\u7528\u6236\u6709\u6548\u5730\u8655\u7406\u8207\u6642\u9593\u76f8\u95dc\u7684\u6578\u64da\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u4e2d\u7684\u65e5\u671f\u51fd\u6578\u8207\u6642\u9593\u51fd\u6578\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"%e6%97%a5%e6%9c%9f%e5%87%bd%e6%95%b8-PvAgajwBTd\">\u65e5\u671f\u51fd\u6578<\/h2>\n<p>MySQL\u7684\u65e5\u671f\u51fd\u6578\u4e3b\u8981\u7528\u65bc\u8655\u7406\u65e5\u671f\u6578\u64da\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65e5\u671f\u51fd\u6578\uff1a<\/p>\n<ul>\n<li><code>CURDATE()<\/code>\uff1a\u8fd4\u56de\u7576\u524d\u65e5\u671f\u3002<\/li>\n<li><code>DATE(NOW())<\/code>\uff1a\u8fd4\u56de\u7576\u524d\u65e5\u671f\uff0c\u6642\u9593\u90e8\u5206\u88ab\u5ffd\u7565\u3002<\/li>\n<li><code>DATE_ADD(date, INTERVAL expr unit)<\/code>\uff1a\u5728\u6307\u5b9a\u7684\u65e5\u671f\u4e0a\u6dfb\u52a0\u4e00\u6bb5\u6642\u9593\u3002<\/li>\n<li><code>DATE_SUB(date, INTERVAL expr unit)<\/code>\uff1a\u5f9e\u6307\u5b9a\u7684\u65e5\u671f\u4e2d\u6e1b\u53bb\u4e00\u6bb5\u6642\u9593\u3002<\/li>\n<li><code>DATEDIFF(date1, date2)<\/code>\uff1a\u8a08\u7b97\u5169\u500b\u65e5\u671f\u4e4b\u9593\u7684\u5929\u6578\u5dee\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%af%84%e4%be%8b-PvAgajwBTd\">\u7bc4\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u9019\u4e9b\u65e5\u671f\u51fd\u6578\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>SELECT CURDATE(); -- \u8fd4\u56de\u7576\u524d\u65e5\u671f\nSELECT DATE(NOW()); -- \u8fd4\u56de\u7576\u524d\u65e5\u671f\uff0c\u6642\u9593\u90e8\u5206\u88ab\u5ffd\u7565\nSELECT DATE_ADD('2023-01-01', INTERVAL 10 DAY); -- \u8fd4\u56de2023-01-11\nSELECT DATE_SUB('2023-01-01', INTERVAL 10 DAY); -- \u8fd4\u56de2022-12-22\nSELECT DATEDIFF('2023-01-01', '2022-12-25'); -- \u8fd4\u56de7\n<\/code><\/pre>\n<h2 id=\"%e6%99%82%e9%96%93%e5%87%bd%e6%95%b8-PvAgajwBTd\">\u6642\u9593\u51fd\u6578<\/h2>\n<p>\u9664\u4e86\u65e5\u671f\u51fd\u6578\uff0cMySQL\u9084\u63d0\u4f9b\u4e86\u591a\u7a2e\u6642\u9593\u51fd\u6578\uff0c\u9019\u4e9b\u51fd\u6578\u4e3b\u8981\u7528\u65bc\u8655\u7406\u6642\u9593\u6578\u64da\u985e\u578b\u3002\u5e38\u7528\u7684\u6642\u9593\u51fd\u6578\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>CURTIME()<\/code>\uff1a\u8fd4\u56de\u7576\u524d\u6642\u9593\u3002<\/li>\n<li><code>TIME(NOW())<\/code>\uff1a\u8fd4\u56de\u7576\u524d\u6642\u9593\uff0c\u65e5\u671f\u90e8\u5206\u88ab\u5ffd\u7565\u3002<\/li>\n<li><code>TIME_FORMAT(time, format)<\/code>\uff1a\u6839\u64da\u6307\u5b9a\u7684\u683c\u5f0f\u8fd4\u56de\u6642\u9593\u3002<\/li>\n<li><code>TIMEDIFF(time1, time2)<\/code>\uff1a\u8a08\u7b97\u5169\u500b\u6642\u9593\u4e4b\u9593\u7684\u5dee\u3002<\/li>\n<li><code>SEC_TO_TIME(seconds)<\/code>\uff1a\u5c07\u79d2\u6578\u8f49\u63db\u70ba\u6642\u9593\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%af%84%e4%be%8b-PvAgajwBTd\">\u7bc4\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u9019\u4e9b\u6642\u9593\u51fd\u6578\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>SELECT CURTIME(); -- \u8fd4\u56de\u7576\u524d\u6642\u9593\nSELECT TIME(NOW()); -- \u8fd4\u56de\u7576\u524d\u6642\u9593\uff0c\u65e5\u671f\u90e8\u5206\u88ab\u5ffd\u7565\nSELECT TIME_FORMAT('15:30:00', '%H:%i:%s'); -- \u8fd4\u56de15:30:00\nSELECT TIMEDIFF('15:30:00', '14:00:00'); -- \u8fd4\u56de1:30:00\nSELECT SEC_TO_TIME(3661); -- \u8fd4\u56de01:01:01\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e5%90%88%e6%97%a5%e6%9c%9f%e8%88%87%e6%99%82%e9%96%93%e5%87%bd%e6%95%b8-PvAgajwBTd\">\u7d50\u5408\u65e5\u671f\u8207\u6642\u9593\u51fd\u6578<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u65e5\u671f\u51fd\u6578\u8207\u6642\u9593\u51fd\u6578\u7d93\u5e38\u9700\u8981\u7d50\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u7576\u9700\u8981\u67e5\u8a62\u67d0\u500b\u7279\u5b9a\u65e5\u671f\u7bc4\u570d\u5167\u7684\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528\u9019\u4e9b\u51fd\u6578\u4f86\u9032\u884c\u7be9\u9078\u3002<\/p>\n<pre><code>SELECT * FROM orders \nWHERE order_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE();\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u67e5\u8a62\u5c07\u8fd4\u56de\u904e\u53bb30\u5929\u5167\u7684\u6240\u6709\u8a02\u55ae\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PvAgajwBTd\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u63d0\u4f9b\u7684\u65e5\u671f\u51fd\u6578\u8207\u6642\u9593\u51fd\u6578\u70ba\u6578\u64da\u5eab\u7ba1\u7406\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u652f\u6301\uff0c\u5e6b\u52a9\u7528\u6236\u6709\u6548\u5730\u8655\u7406\u8207\u6642\u9593\u76f8\u95dc\u7684\u6578\u64da\u3002\u7121\u8ad6\u662f\u5728\u6578\u64da\u5206\u6790\u3001\u5831\u544a\u751f\u6210\u9084\u662f\u65e5\u5e38\u7684\u6578\u64da\u64cd\u4f5c\u4e2d\uff0c\u9019\u4e9b\u51fd\u6578\u90fd\u80fd\u5920\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u5728\u60a8\u7684\u9805\u76ee\u4e2d\u4f7f\u7528\u9019\u4e9b\u529f\u80fd\uff0c\u6216\u662f\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22MySQL\u6578\u64da\u5eab\u4e2d\u7684\u65e5\u671f\u548c\u6642\u9593\u51fd\u6578\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u8655\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u6642\u9593\u6578\u64da\u4ee5\u63d0\u5347\u6578\u64da\u7ba1\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-76456","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=76456"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76456\/revisions"}],"predecessor-version":[{"id":76457,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76456\/revisions\/76457"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}