{"id":200865,"date":"2025-05-06T13:46:21","date_gmt":"2025-05-06T05:46:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200865\/"},"modified":"2025-05-06T13:46:21","modified_gmt":"2025-05-06T05:46:21","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-sql-%e6%9f%a5%e8%af%a2%e5%9c%a8%e7%89%b9%e5%ae%9a%e6%97%b6%e9%97%b4%e8%8c%83%e5%9b%b4%e5%86%85%ef%bc%8c%e8%ae%b0%e5%bd%95%e6%95%b0%e8%b6%85%e8%bf%87%e9%98%88%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200865\/","title":{"rendered":"\u5982\u4f55\u7528 SQL \u67e5\u8be2\u5728\u7279\u5b9a\u65f6\u95f4\u8303\u56f4\u5185\uff0c\u8bb0\u5f55\u6570\u8d85\u8fc7\u9608\u503c\u7684 item_ID\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7528 SQL \u67e5\u8be2\u5728\u7279\u5b9a\u65f6\u95f4\u8303\u56f4\u5185\uff0c\u8bb0\u5f55\u6570\u8d85\u8fc7\u9608\u503c\u7684 item_ID\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731903887673ac18f958ff.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>sql \u7edf\u8ba1 5 \u79d2\u5185\u8bb0\u5f55\u6570\u8d85\u8fc7 5 \u7684\u503c<\/strong><\/p>\n<p>\u5728\u5173\u8054\u8868\u4e2d\uff0c\u53ef\u80fd\u5b58\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u7279\u5b9a\u503c\u51fa\u73b0\u7684\u9891\u7387\u5f88\u9ad8\u7684\u60c5\u51b5\u3002\u4e3a\u4e86\u8bc6\u522b\u8fd9\u4e9b\u9891\u7e41\u51fa\u73b0\u7684\u503c\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7edf\u8ba1\u7279\u5b9a\u65f6\u95f4\u6bb5\u5185\u8bb0\u5f55\u6570\u8d85\u8fc7\u9608\u503c\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u7ed9\u5b9a\u4ee5\u4e0b\u8bb0\u5f55\u8868\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>item_id<\/th>\n<th>time<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>1<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>1<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>1<\/td>\n<td>8<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>2<\/td>\n<td>8<\/td>\n<\/tr>\n<tr>\n<td>11<\/td>\n<td>1<\/td>\n<td>8<\/td>\n<\/tr>\n<tr>\n<td>12<\/td>\n<td>1<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>13<\/td>\n<td>2<\/td>\n<td>11<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u4eec\u60f3\u7edf\u8ba1\u51fa\u5728 5 \u79d2\u65f6\u95f4\u8303\u56f4\u5185\uff0c\u8bb0\u5f55\u6570\u8d85\u8fc7 5 \u7684 item_id\u3002\u5177\u4f53\u800c\u8a00\uff0c\u5728\u7b2c 4 \u79d2\u5230\u7b2c 8 \u79d2\u671f\u95f4\uff0citem_id \u4e3a 1 \u7684\u8bb0\u5f55\u51fa\u73b0 6 \u6b21\uff0c\u56e0\u6b64\u5e94\u5305\u542b\u5728\u7ed3\u679c\u4e2d\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b sql \u67e5\u8be2\uff1a<\/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>\u6267\u884c\u6b64\u67e5\u8be2\u7684\u7ed3\u679c\u4e3a\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>item_id<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u4e3b\u673a\u5b9d\u8d1d\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 SQL \u67e5\u8be2\u5728\u7279\u5b9a\u65f6\u95f4\u8303&#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-200865","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200865","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=200865"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200865\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}