{"id":7242,"date":"2024-11-14T11:03:47","date_gmt":"2024-11-14T03:03:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7242\/"},"modified":"2024-11-14T11:03:47","modified_gmt":"2024-11-14T03:03:47","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:\/\/fwq.ai\/blog\/7242\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173062945159368.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL \u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\u67e5\u8be2\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL \u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\u67e5\u8be2\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u4f18\u5316 MySQL \u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u5c42\u7ea7\u67e5\u8be2\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>\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e\u7684\u4f18\u5316 \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 \u95ed\u5305\u8868 \u5efa\u8bae\u4f7f\u7528\u95ed\u5305\u8868\u6765\u4f18\u5316\u6b64\u67e5\u8be2\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a create table tree( self int, parent int, distance int ); \u767b\u5f55\u540e\u590d\u5236 \u63d2\u5165\u793a\u4f8b\u6570\u636e\u5e76\u6309 self \u5206\u7c7b\u5408\u5e76\u5f62\u6210\u6811\uff1a from_database = [ [&#8220;\u9e21\u8089&#8221;, &#8220;\u9e21\u8089&#8221;, 0], [&#8220;\u9e21\u8089&#8221;, &#8220;\u8089\u7c7b&#8221;, 1], [&#8220;\u9e21\u8089&#8221;, &#8220;\u98df\u7269&#8221;, 2], [&#8220;\u8089\u7c7b&#8221;, &#8220;\u8089\u7c7b&#8221;, 0], [&#8220;\u8089\u7c7b&#8221;, &#8220;\u98df\u7269&#8221;, 1], ] from itertools import groupby root = {} for _, path in groupby(from_database, key=lambda x: x[0]): path = sorted(list(path), [&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-7242","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7242","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=7242"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7242\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}