{"id":6825,"date":"2024-11-14T14:16:27","date_gmt":"2024-11-14T06:16:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6825\/"},"modified":"2024-11-14T14:16:27","modified_gmt":"2024-11-14T06:16:27","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:\/\/fwq.ai\/blog\/6825\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172995213554504.jpg\" class=\"aligncenter\" title=\"\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\u63d2\u56fe\" alt=\"\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\u63d2\u56fe\" \/><\/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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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\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\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sql \u7edf\u8ba1 5 \u79d2\u5185\u8bb0\u5f55\u6570\u8d85\u8fc7 5 \u7684\u503c \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 \u4f8b\u5982\uff0c\u7ed9\u5b9a\u4ee5\u4e0b\u8bb0\u5f55\u8868\uff1a id item_id time 1 1 1 2 2 2 3 2 2 4 2 3 5 2 3 6 1 4 7 1 7 8 1 7 9 1 8 10 2 8 11 1 8 12 1 9 13 2 11 \u6211\u4eec\u60f3\u7edf\u8ba1\u51fa\u5728 5 \u79d2\u65f6\u95f4\u8303\u56f4\u5185\uff0c\u8bb0\u5f55\u6570\u8d85\u8fc7 5 \u7684 [&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-6825","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6825","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=6825"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6825\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}