{"id":6771,"date":"2024-11-14T14:39:59","date_gmt":"2024-11-14T06:39:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6771\/"},"modified":"2024-11-14T14:39:59","modified_gmt":"2024-11-14T06:39:59","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:\/\/fwq.ai\/blog\/6771\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172991252165466.jpg\" class=\"aligncenter\" title=\"\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\u63d2\u56fe\" alt=\"\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\u63d2\u56fe\" \/><\/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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\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\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>\u5982\u4f55\u7edf\u8ba1\u6307\u5b9a\u65f6\u95f4\u5185\u8bb0\u5f55\u8d85\u8fc7\u6307\u5b9a\u503c \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 \u95ee\u9898 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684\u8bb0\u5f55\u8868\uff1a +&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+ | id | item_id | time | +&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+ | 1 | 1 | 1 | | 2 | 2 | 2 | | 3 | 2 | 2 | | 4 | 2 | 3 | | 5 | 2 | 3 | | 6 | 1 | [&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-6771","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6771","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=6771"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6771\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}