{"id":201052,"date":"2025-05-06T11:02:32","date_gmt":"2025-05-06T03:02:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201052\/"},"modified":"2025-05-06T11:02:32","modified_gmt":"2025-05-06T03:02:32","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-sql-%e6%9f%a5%e8%af%a2%e7%bb%9f%e8%ae%a1%e6%8c%87%e5%ae%9a%e6%97%b6%e9%97%b4%e5%86%85%e8%ae%b0%e5%bd%95%e6%95%b0%e9%87%8f%e8%b6%85%e8%bf%87%e6%8c%87%e5%ae%9a%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201052\/","title":{"rendered":"\u5982\u4f55\u7528 SQL \u67e5\u8be2\u7edf\u8ba1\u6307\u5b9a\u65f6\u95f4\u5185\u8bb0\u5f55\u6570\u91cf\u8d85\u8fc7\u6307\u5b9a\u503c\u7684 item_ID\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7528 SQL \u67e5\u8be2\u7edf\u8ba1\u6307\u5b9a\u65f6\u95f4\u5185\u8bb0\u5f55\u6570\u91cf\u8d85\u8fc7\u6307\u5b9a\u503c\u7684 item_ID\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/17327552616747bf3d4ee45.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u7edf\u8ba1\u6307\u5b9a\u65f6\u95f4\u5185\u8bb0\u5f55\u8d85\u8fc7\u6307\u5b9a\u503c<\/strong><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u79cd sql \u67e5\u8be2\uff0c\u7528\u4e8e\u7edf\u8ba1\u6307\u5b9a\u65f6\u95f4\u5185\u8bb0\u5f55\u6570\u91cf\u8d85\u8fc7\u6307\u5b9a\u503c\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u95ee\u9898<\/strong><\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684\u8bb0\u5f55\u8868\uff1a<\/p>\n<pre>+-----+--------+------+\n| id  | item_id | time  |\n+-----+--------+------+\n| 1   | 1       | 1     |\n| 2   | 2       | 2     |\n| 3   | 2       | 2     |\n| 4   | 2       | 3     |\n| 5   | 2       | 3     |\n| 6   | 1       | 4     |\n| 7   | 1       | 7     |\n| 8   | 1       | 7     |\n| 9   | 1       | 8     |\n| 10  | 2       | 8     |\n| 11  | 1       | 8     |\n| 12  | 1       | 9     |\n| 13  | 2       | 11    |\n+-----+--------+------+<\/pre>\n<p>\u6211\u4eec\u7684\u76ee\u6807\u662f\u7edf\u8ba1\u5728 5 \u79d2\u5185\u8bb0\u5f55\u6570\u91cf\u8d85\u8fc7 5 \u4e2a\u7684 item_id\u3002\u6839\u636e\u4e0a\u8868\uff0c\u67e5\u8be2\u7ed3\u679c\u5e94\u8be5\u662f\uff1a<\/p>\n<pre>+--------+\n| item_id |\n+--------+\n| 1       |\n+--------+<\/pre>\n<p>\u56e0\u4e3a\u4ece\u7b2c 4 \u79d2\u5f00\u59cb\u7684 5 \u79d2\u5185\u6709 6 \u4e2a item_id = 1 \u7684\u8bb0\u5f55\u3002<\/p>\n<p><strong>sql \u67e5\u8be2<\/strong><\/p>\n<pre>CREATE DATABASE test;\nuse test;\nCREATE TABLE tableName\n(\n     ID     varchar(300),\n     item_ID     varchar(300),\n     time     varchar(255)\n);\n\nINSERT INTO tableName ( ID , item_ID , time )\nVALUES\n    ('1', '1', '1'),\n    ('2', '2', '2'),\n    ('3', '2', '2'),\n    ('4', '2', '3'),\n    ('5', '2', '3'),\n    ('6', '1', '4'),\n    ('7', '1', '7'),\n    ('8', '1', '7'),\n    ('9', '1', '8'),\n    ('10', '2', '8'),\n    ('11', '1', '8'),\n    ('12', '1', '9'),\n    ('13', '2', '11');\nSELECT DISTINCT c.item_ID\nFROM \n    (SELECT \n        (SELECT b.item_ID\n        FROM tableName b\n        WHERE b.item_ID = a.item_ID\n                AND b.time\n            BETWEEN a.time\n                AND (a.time + 5)\n        GROUP BY  b.item_ID\n        HAVING count(b.item_ID) &gt;5 ) item_ID\n        FROM tableName a\n        ORDER BY  a.time) c\n    WHERE c.item_ID is NOT null;<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 SQL \u67e5\u8be2\u7edf\u8ba1\u6307\u5b9a\u65f6\u95f4&#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-201052","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201052","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=201052"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201052\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}