{"id":200858,"date":"2025-05-06T13:01:24","date_gmt":"2025-05-06T05:01:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200858\/"},"modified":"2025-05-06T13:01:24","modified_gmt":"2025-05-06T05:01:24","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-mysql-%e7%bb%9f%e8%ae%a1%e4%b8%80%e5%a4%a9%e6%95%b0%e6%8d%ae%e9%87%8f%ef%bc%8c%e5%b9%b6%e5%b0%86%e5%85%b6%e5%88%92%e5%88%86%e4%b8%ba-5-%e5%88%86%e9%92%9f%e4%b8%80%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200858\/","title":{"rendered":"\u5982\u4f55\u7528 MySQL \u7edf\u8ba1\u4e00\u5929\u6570\u636e\u91cf\uff0c\u5e76\u5c06\u5176\u5212\u5206\u4e3a 5 \u5206\u949f\u4e00\u4e2a\u533a\u95f4\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7528 MySQL \u7edf\u8ba1\u4e00\u5929\u6570\u636e\u91cf\uff0c\u5e76\u5c06\u5176\u5212\u5206\u4e3a 5 \u5206\u949f\u4e00\u4e2a\u533a\u95f4\uff1f<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/17318471866739e4128cf8e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u9ad8\u6548\u7edf\u8ba1\u4e00\u5929\u6570\u636e\u91cf\uff0c\u52065\u5206\u949f\u4e3a\u4e00\u4e2a\u533a\u95f4<\/strong><\/p>\n<p>mysql\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u6309\u65f6\u95f4\u6bb5\u7edf\u8ba1\u6570\u636e\u91cf\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5c06\u4e00\u5929\u5212\u5206\u4e3a5\u5206\u949f\u4e00\u4e2a\u533a\u95f4\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u533a\u95f4\u5185\u7684\u6570\u636e\u91cf\u3002<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u5f20\u8f85\u52a9\u8868time_intervals\uff0c\u7528\u4e8e\u5b58\u50a8\u65f6\u95f4\u6bb5\uff1a<\/p>\n<pre>create table `time_intervals` (`grouped_time` time default null)<\/pre>\n<p>\u63a5\u7740\uff0c\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\u5411time_intervals\u8868\u4e2d\u63d2\u5165\u65f6\u95f4\u6bb5\uff1a<\/p>\n<pre>delimiter \/\/\ncreate procedure inserttimeintervals()\nbegin\n  declare currenttime time default '00:00:00';\n  declare endtime time default '23:55:00';\n  truncate table time_intervals;\n  while currenttime &lt;= endtime do\n    insert into time_intervals (grouped_time) values (currenttime);\n    set currenttime = addtime(currenttime, '00:05:00');\n  end while;\nend \/\/\ndelimiter ;<\/pre>\n<p>\u6267\u884c\u5b58\u50a8\u8fc7\u7a0bcall inserttimeintervals();\uff0c\u751f\u6210288\u6761\u65f6\u95f4\u6bb5\u7528\u4e8e\u7edf\u8ba1\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u67e5\u8be2\u5b9e\u9645\u6570\u636e\u5e76\u88650\uff1a<\/p>\n<pre>(select\n  date_format(date_format(create_time, '%y-%m-%d %h:%i') - interval (minute(create_time) mod 5) minute, '%h:%i') as grouped_time,\n  count(*) as counts\nfrom\n  interface_access_frequency\nwhere\n  date(create_time) = '2024-04-27'\ngroup by\n  grouped_time)\nunion\nselect\n  date_format(grouped_time, '%h:%i') as grouped_time,\n  0 as counts\nfrom\n  time_intervals;<\/pre>\n<p>\u6700\u540e\uff0cgroupby\u5206\u7ec4\uff0c\u5373\u53ef\u83b7\u5f97\u6bcf\u4e2a5\u5206\u949f\u533a\u95f4\u7684\u7edf\u8ba1\u7ed3\u679c\uff1a<\/p>\n<pre>SELECT grouped_time, MAX(counts) AS counts\nFROM\n  ((SELECT\n    DATE_FORMAT(DATE_FORMAT(create_time, '%Y-%m-%d %H:%i') - INTERVAL (MINUTE(create_time) MOD 5) MINUTE, '%H:%i') AS grouped_time,\n    COUNT(*) AS counts\n  FROM\n    interface_access_frequency\n  WHERE\n    DATE(create_time) = '2024-04-27'\n  GROUP BY\n    grouped_time)\n  UNION\n  SELECT\n    DATE_FORMAT(grouped_time, '%H:%i') AS grouped_time,\n    0 AS counts\n  FROM\n    time_intervals) AS a\nGROUP BY\n  grouped_time\nORDER BY\n  grouped_time ASC;<\/pre>\n<p>\u901a\u8fc7\u6b64\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u63095\u5206\u949f\u533a\u95f4\u7edf\u8ba1\u4e00\u5929\u7684\u6570\u636e\u91cf\uff0c\u4e14\u56fa\u5b9a\u7ed3\u679c\u4e3a288\u3002\u4fee\u6539\u4f20\u53c2\uff0c\u8fd8\u53ef\u6309\u4efb\u610f\u65f6\u95f4\u533a\u95f4\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u7528 MySQL \u7edf\u8ba1\u4e00\u5929\u6570\u636e\u91cf\uff0c\u5e76\u5c06\u5176\u5212\u5206\u4e3a 5 \u5206\u949f\u4e00\u4e2a\u533a\u95f4\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 MySQL \u7edf\u8ba1\u4e00\u5929\u6570\u636e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200858","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200858","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200858"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200858\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}