{"id":200494,"date":"2025-05-06T09:36:01","date_gmt":"2025-05-06T01:36:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200494\/"},"modified":"2025-05-06T09:36:01","modified_gmt":"2025-05-06T01:36:01","slug":"%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%9c%b0%e4%bb%8e%e5%a4%a7%e5%9e%8b-mysql-%e8%a1%a8%e4%b8%ad%e6%9f%a5%e8%af%a2%e6%8c%87%e5%ae%9a%e6%97%b6%e9%97%b4%e5%b7%ae%e7%9a%84%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200494\/","title":{"rendered":"\u5982\u4f55\u9ad8\u6548\u5730\u4ece\u5927\u578b MySQL \u8868\u4e2d\u67e5\u8be2\u6307\u5b9a\u65f6\u95f4\u5dee\u7684\u6570\u636e\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u9ad8\u6548\u5730\u4ece\u5927\u578b MySQL \u8868\u4e2d\u67e5\u8be2\u6307\u5b9a\u65f6\u95f4\u5dee\u7684\u6570\u636e\uff1f<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6570\u636e\u5e93\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u9ad8\u6548\u5730\u4ece\u5927\u578b MySQL \u8868\u4e2d\u67e5\u8be2\u6307\u5b9a\u65f6\u95f4\u5dee\u7684\u6570\u636e\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241106\/1730889586672b4772add41.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql\u67e5\u8be2\u6307\u5b9a\u65f6\u95f4\u5dee\u7684\u6570\u636e<\/strong><\/p>\n<p>\u5f53\u5904\u7406\u5927\u578b\u8868\u65f6\uff0c\u627e\u51fa\u5b58\u5728\u65f6\u95f4\u5dee\u7684\u6570\u636e\u53ef\u80fd\u9887\u5177\u6311\u6218\u3002\u5982\u679c\u6ca1\u6709\u4fbf\u6377\u7684\u5f00\u7a97\u51fd\u6570\u53ef\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf<\/strong><\/p>\n<pre>set @tmp = '2000-1-1';\nselect * from (\n    select *,\n    timestampdiff(second, @tmp, time) as diff,\n    @tmp := time\n    from \u8868\u540d\n) as t1\nwhere diff &gt; 300;<\/pre>\n<p>\u6b64\u65b9\u6cd5\u901a\u8fc7\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf @tmp \u4fdd\u5b58\u4e0a\u4e00\u6761\u8bb0\u5f55\u7684\u65f6\u95f4\u5e76\u8ba1\u7b97\u5176\u4e0e\u5f53\u524d\u8bb0\u5f55\u7684\u65f6\u95f4\u5dee\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4ea4\u53c9\u8fde\u63a5<\/strong><\/p>\n<p>\u5982\u679c\u53ef\u4ee5\u751f\u6210\u65b0\u8868\uff0c\u5219\u53ef\u4ee5\u521b\u5efa\u81ea\u52a8\u589e\u91cf\u4e3b\u952e\u5b57\u6bb5\uff0c\u7136\u540e\u4f7f\u7528\u4ea4\u53c9\u8fde\u63a5\uff1a<\/p>\n<pre>select *\nfrom \u8868 as a\nleft join \u8868 as b\non a.\u4e3b\u952e = b.\u4e3b\u952e + 1\nwhere timestampdiff(second, a.time, b.time) &gt; 300;<\/pre>\n<p>\u6b64\u65b9\u6cd5\u5c06\u5f53\u524d\u8bb0\u5f55\u4e0e\u540e\u7eed\u8bb0\u5f55\u4ea4\u53c9\u8fde\u63a5\uff0c\u5e76\u6bd4\u8f83\u65f6\u95f4\u5dee\u3002<\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 lag \u53d8\u91cf<\/strong><\/p>\n<p>\u867d\u7136\u539f\u59cb\u95ee\u9898\u4e2d\u6307\u51fa\u4e0d\u652f\u6301 lag \u5f00\u7a97\u51fd\u6570\uff0c\u4f46\u4ecd\u53ef\u901a\u8fc7\u590d\u5236\u8868\u6765\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\uff1a<\/p>\n<pre>set @i = 0; set @j = 0;\nselect *, a2 - a1\nfrom (\n    select *, @i := @i + 1 as a1\n    from lag1\n) as t1\nleft join (\n    select *, @j := @j + 1 as a2\n    from lag1\n) as t2\non t1.a1 + 1 = t2.a2 ;<\/pre>\n<p>\u6b64\u65b9\u6cd5\u901a\u8fc7\u751f\u6210\u4e24\u4e2a\u8f85\u52a9\u8868\u5e76\u4f7f\u7528\u8fde\u63a5\u6765\u6a21\u62df lag \u53d8\u91cf\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u9ad8\u6548\u5730\u4ece\u5927\u578b MySQL \u8868\u4e2d\u67e5\u8be2\u6307\u5b9a\u65f6\u95f4\u5dee\u7684\u6570\u636e\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u9ad8\u6548\u5730\u4ece\u5927\u578b MySQL \u8868&#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-200494","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200494","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=200494"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200494\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}