{"id":27292,"date":"2024-11-24T17:34:33","date_gmt":"2024-11-24T09:34:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27292\/"},"modified":"2024-11-24T17:34:33","modified_gmt":"2024-11-24T09:34:33","slug":"mysql-%e6%97%a5%e6%9c%9f%e5%8c%b9%e9%85%8d%e4%b8%8e%e9%9a%8f%e6%9c%ba%e6%9c%88%e4%bb%bd%e6%9f%a5%e8%af%a2%ef%bc%9a%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e6%95%b0%e6%8d%ae%e9%94%99%e4%b9%b1%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27292\/","title":{"rendered":"MySQL \u65e5\u671f\u5339\u914d\u4e0e\u968f\u673a\u6708\u4efd\u67e5\u8be2\uff1a\u5982\u4f55\u907f\u514d\u6570\u636e\u9519\u4e71\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173122526322727.jpg\" class=\"aligncenter\" title=\"MySQL \u65e5\u671f\u5339\u914d\u4e0e\u968f\u673a\u6708\u4efd\u67e5\u8be2\uff1a\u5982\u4f55\u907f\u514d\u6570\u636e\u9519\u4e71\uff1f\u63d2\u56fe\" alt=\"MySQL \u65e5\u671f\u5339\u914d\u4e0e\u968f\u673a\u6708\u4efd\u67e5\u8be2\uff1a\u5982\u4f55\u907f\u514d\u6570\u636e\u9519\u4e71\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u65e5\u671f\u5339\u914d\u4e0e\u968f\u673a\u6708\u4efd\u67e5\u8be2\u96be\u9898<\/strong><\/p>\n<p>\u67e5\u8be2\u4ece\u6307\u5b9a\u6708\u4efd\u5230\u73b0\u5728\u7684\u65f6\u95f4\u6bb5\u5185\uff0c\u5e76\u968f\u673a\u9009\u62e9\u67d0\u4e2a\u6708\u4efd\u7684\u6570\u636e\uff0c\u5374\u9047\u5230\u4e86\u6570\u636e\u9519\u4e71\u7684\u95ee\u9898\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u539f\u59cb sql \u4e2d\u7684 rand() \u51fd\u6570\u5728\u6bcf\u6b21 where \u6761\u4ef6\u4e2d\u6267\u884c\uff0c\u5bfc\u81f4\u65e5\u671f\u8303\u56f4\u4e0d\u65ad\u53d8\u5316\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 mysql 8 \u4e2d\u7684 with \u8bed\u53e5\uff1a<\/p>\n<pre>with mo1 as (\n    select date_format(date_add('2023-11-01', interval floor(rand() * datediff(curdate(), '2023-11-01')) day), '%y-%m') as month\n)\n\nselect *\nfrom teacher\njoin mo1 on mo1.month = date_format(create_time, '%y-%m')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u5316\u5efa\u8bae<\/strong><\/p>\n<p>\u867d\u7136 with \u8bed\u53e5\u89e3\u51b3\u4e86\u95ee\u9898\uff0c\u4f46\u8fd8\u6709\u4ee5\u4e0b\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<ul>\n<li>\u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u751f\u6210\u968f\u673a\u65e5\u671f\u8303\u56f4\uff0c\u907f\u514d\u591a\u6b21\u6267\u884c rand() \u51fd\u6570\u3002<\/li>\n<li>\u4f7f\u7528 between \u6216\u5927\u4e8e\u7b49\u4e8e\/\u5c0f\u4e8e\u67e5\u8be2\u6761\u4ef6\uff0c\u907f\u514d\u4f7f\u7528 date_format() \u51fd\u6570\uff0c\u4ee5\u5229\u7528\u7d22\u5f15\u3002<\/li>\n<li>\u4e3a create_time \u5217\u521b\u5efa\u7d22\u5f15\uff0c\u4ee5\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4f18\u5316\u540e\u7684 sql\uff1a<\/p>\n<pre>SELECT *\nFROM teacher\nWHERE create_time BETWEEN '2024-01-01 00:00:00' AND '2024-01-31 23:59:59';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u65e5\u671f\u5339\u914d\u4e0e\u968f\u673a\u6708\u4efd\u67e5\u8be2\uff1a\u5982\u4f55\u907f\u514d\u6570\u636e\u9519\u4e71\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65e5\u671f\u5339\u914d\u4e0e\u968f\u673a\u6708\u4efd\u67e5\u8be2\u96be\u9898 \u67e5\u8be2\u4ece\u6307\u5b9a\u6708\u4efd\u5230\u73b0\u5728\u7684\u65f6\u95f4\u6bb5\u5185\uff0c\u5e76\u968f\u673a\u9009\u62e9\u67d0\u4e2a\u6708\u4efd\u7684\u6570\u636e\uff0c\u5374\u9047\u5230\u4e86\u6570\u636e\u9519\u4e71\u7684\u95ee\u9898\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u539f\u59cb sql \u4e2d\u7684 rand() \u51fd\u6570\u5728\u6bcf\u6b21 where \u6761\u4ef6\u4e2d\u6267\u884c\uff0c\u5bfc\u81f4\u65e5\u671f\u8303\u56f4\u4e0d\u65ad\u53d8\u5316\u3002 \u89e3\u51b3\u65b9\u6848 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 mysql 8 \u4e2d\u7684 with \u8bed\u53e5\uff1a with mo1 as ( select date_format(date_add(&#8216;2023-11-01&#8217;, interval floor(rand() * datediff(curdate(), &#8216;2023-11-01&#8217;)) day), &#8216;%y-%m&#8217;) as month ) select * from teacher join mo1 on mo1.month = date_format(create_time, &#8216;%y-%m&#8217;) \u767b\u5f55\u540e\u590d\u5236 \u4f18\u5316\u5efa\u8bae \u867d\u7136 with \u8bed\u53e5\u89e3\u51b3\u4e86\u95ee\u9898\uff0c\u4f46\u8fd8\u6709\u4ee5\u4e0b\u4f18\u5316\u5efa\u8bae\uff1a \u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u751f\u6210\u968f\u673a\u65e5\u671f\u8303\u56f4\uff0c\u907f\u514d\u591a\u6b21\u6267\u884c rand() \u51fd\u6570\u3002 \u4f7f\u7528 between \u6216\u5927\u4e8e\u7b49\u4e8e\/\u5c0f\u4e8e\u67e5\u8be2\u6761\u4ef6\uff0c\u907f\u514d\u4f7f\u7528 date_format() \u51fd\u6570\uff0c\u4ee5\u5229\u7528\u7d22\u5f15\u3002 [&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-27292","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27292","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=27292"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27292\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}