{"id":200889,"date":"2025-05-06T10:21:27","date_gmt":"2025-05-06T02:21:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200889\/"},"modified":"2025-05-06T10:21:27","modified_gmt":"2025-05-06T02:21:27","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%97%ad%e5%8c%85%e8%a1%a8%e4%bc%98%e5%8c%96-mysql-%e6%a0%91%e7%8a%b6%e7%bb%93%e6%9e%84%e6%95%b0%e6%8d%ae%e7%9a%84%e5%b1%82%e7%ba%a7%e6%9f%a5%e8%af%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200889\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL \u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\u67e5\u8be2\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL \u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\u67e5\u8be2\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL \u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\u67e5\u8be2\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731992263673c1ac72ddb1.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql \u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u4f18\u5316<\/strong><\/p>\n<p>\u8981\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u9012\u5f52\u6216\u95ed\u5305\u8868\u7b49\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u95ed\u5305\u8868<\/strong><\/p>\n<p>\u5efa\u8bae\u4f7f\u7528\u95ed\u5305\u8868\u6765\u4f18\u5316\u6b64\u67e5\u8be2\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>create table tree(\n  self int,\n  parent int,\n  distance int\n);<\/pre>\n<p>\u63d2\u5165\u793a\u4f8b\u6570\u636e\u5e76\u6309 self \u5206\u7c7b\u5408\u5e76\u5f62\u6210\u6811\uff1a<\/p>\n<pre>from_database = [\n    [\"\u9e21\u8089\", \"\u9e21\u8089\", 0],\n    [\"\u9e21\u8089\", \"\u8089\u7c7b\", 1],\n    [\"\u9e21\u8089\", \"\u98df\u7269\", 2],\n    [\"\u8089\u7c7b\", \"\u8089\u7c7b\", 0],\n    [\"\u8089\u7c7b\", \"\u98df\u7269\", 1],\n]\nfrom itertools import groupby\n\nroot = {}\nfor _, path in groupby(from_database, key=lambda x: x[0]):\n    path = sorted(list(path), key=lambda x: -x[2])\n    node = root\n    for _, nodename, _ in path:\n        node = node.setdefault(nodename, {})\nprint(root)  # {'\u98df\u7269': {'\u8089\u7c7b': {'\u9e21\u8089': {}}}}<\/pre>\n<p><strong>\u67e5\u8be2<\/strong><\/p>\n<p>\u4f7f\u7528 like \u67e5\u8be2\u5e26\u8089\u7684\u5b57\u6bb5\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>select * from tree where self like '%\u8089%';<\/pre>\n<pre>+------+---------+----------+\n| self | parent  | distance |\n+------+---------+----------+\n| \u8089\u7c7b | \u8089\u7c7b | 0 |\n| \u8089\u7c7b | \u98df\u7269 | 1 |\n| \u9e21\u8089 | \u9e21\u8089 | 0 |\n| \u9e21\u8089 | \u8089\u7c7b | 1 |\n| \u9e21\u8089 | \u98df\u7269 | 2 |\n+------+--------+----------+<\/pre>\n<p>\u8be5\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\uff0c\u5b83\u53ef\u4ee5\u975e\u5e38\u9ad8\u6548\u5730\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6269\u5c55\u5230\u67e5\u8be2\u66f4\u591a\u5c42\u7ea7\u7684\u5c42\u7ea7\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL \u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\u67e5\u8be2\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL &#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-200889","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200889","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=200889"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200889\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}