{"id":6998,"date":"2024-11-14T13:07:19","date_gmt":"2024-11-14T05:07:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6998\/"},"modified":"2024-11-14T13:07:19","modified_gmt":"2024-11-14T05:07:19","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%97%ad%e5%8c%85%e8%a1%a8%e9%ab%98%e6%95%88%e6%9f%a5%e8%af%a2%e5%b8%a6%e6%9c%89%e7%88%b6%e5%ad%90%e5%85%b3%e7%b3%bb%e7%9a%84%e6%a0%91%e7%8a%b6%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6998\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u9ad8\u6548\u67e5\u8be2\u5e26\u6709\u7236\u5b50\u5173\u7cfb\u7684\u6811\u72b6\u7ed3\u6784\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173026370656912.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u9ad8\u6548\u67e5\u8be2\u5e26\u6709\u7236\u5b50\u5173\u7cfb\u7684\u6811\u72b6\u7ed3\u6784\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u9ad8\u6548\u67e5\u8be2\u5e26\u6709\u7236\u5b50\u5173\u7cfb\u7684\u6811\u72b6\u7ed3\u6784\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e<\/strong><\/p>\n<p>\u5f53\u9047\u5230\u5e26\u6709\u7236\u7ea7-\u5b50\u7ea7\u5173\u7cfb\u7684\u6570\u636e\u65f6\uff0c\u4f8b\u5982\u5e26\u6709 id\u3001parent_id \u548c name \u5b57\u6bb5\u7684\u8868\uff0c\u5e76\u5e0c\u671b\u6839\u636e name \u6a21\u7cca\u67e5\u8be2\u76f8\u5173\u5c42\u7ea7\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u8868\u6765\u89e3\u51b3\u3002<\/p>\n<p><strong>\u95ed\u5305\u8868<\/strong><\/p>\n<p>\u95ed\u5305\u8868\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u8868\uff0c\u5b83\u5b58\u50a8\u4e86\u6240\u6709\u8282\u70b9\u53ca\u5176\u5230\u6839\u8282\u70b9\u7684\u8ddd\u79bb\u3002\u4f7f\u7528\u95ed\u5305\u8868\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\u7684\u4e3b\u8981\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u6811\u8868\uff0c\u5b58\u50a8\u8282\u70b9\u53ca\u5176\u5230\u6839\u8282\u70b9\u7684\u8ddd\u79bb\u3002<\/li>\n<li>\u6839\u636e name \u6a21\u7cca\u67e5\u8be2\u95ed\u5305\u8868\uff0c\u83b7\u53d6\u76f8\u5173\u8282\u70b9\u3002<\/li>\n<li>\u6839\u636e parent \u548c distance \u5b57\u6bb5\uff0c\u5c06\u67e5\u8be2\u5230\u7684\u8282\u70b9\u7ec4\u5408\u4e3a\u6811\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6811\u72b6\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n<pre>----\u98df\u7269\n   ---- \u6c34\u679c\n     ---- \u9999\u8549\n     ---- \u82f9\u679c\n   ---- \u8089\u7c7b\n     ---- \u9e21\u8089\n     ---- \u80a5\u725b<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u82e5\u8981\u67e5\u8be2\u5305\u542b &#8220;\u8089&#8221; \u7684\u6240\u6709\u8282\u70b9\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>select * from tree where self like '%\u8089%';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u67e5\u8be2\u7ed3\u679c\u5982\u4e0b\uff1a<\/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>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u7ec4\u548c\u5408\u5e76\u7684\u65b9\u5f0f\uff0c\u5c06\u67e5\u8be2\u5230\u7684\u8282\u70b9\u7ec4\u88c5\u6210\u6811\u7ed3\u6784\uff1a<\/p>\n<pre>from_datebase = [\n    [\"\u9e21\u8089\", \"\u9e21\u8089\", 0],\n    [\"\u9e21\u8089\", \"\u8089\u7c7b\", 1],\n    [\"\u9e21\u8089\", \"\u98df\u7269\", 2],\n\n    [\"\u8089\u7c7b\", \"\u8089\u7c7b\", 0],\n    [\"\u8089\u7c7b\", \"\u98df\u7269\", 1],\n]\nfrom itertools import groupby\n\nroot = {}\n\nfor _, path in groupby(from_datebase, 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, {})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u7ec8\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>{'\u98df\u7269': {'\u8089\u7c7b': {'\u9e21\u8089': {}}}}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u4f7f\u7528 id \u800c\u4e0d\u662f\u4e2d\u6587\u540d\u79f0\u6765\u8868\u793a\u8282\u70b9\u3002<\/li>\n<li>\u95ed\u5305\u8868\u7684\u5b58\u50a8\u7a7a\u95f4\u4f1a\u968f\u7740\u5c42\u7ea7\u7684\u589e\u52a0\u800c\u589e\u957f\u3002<\/li>\n<li>\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u9700\u8981\u7279\u6b8a\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u8868\u9ad8\u6548\u67e5\u8be2\u5e26\u6709\u7236\u5b50\u5173\u7cfb\u7684\u6811\u72b6\u7ed3\u6784\u6570\u636e\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 \u5f53\u9047\u5230\u5e26\u6709\u7236\u7ea7-\u5b50\u7ea7\u5173\u7cfb\u7684\u6570\u636e\u65f6\uff0c\u4f8b\u5982\u5e26\u6709 id\u3001parent_id \u548c name \u5b57\u6bb5\u7684\u8868\uff0c\u5e76\u5e0c\u671b\u6839\u636e name \u6a21\u7cca\u67e5\u8be2\u76f8\u5173\u5c42\u7ea7\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u8868\u6765\u89e3\u51b3\u3002 \u95ed\u5305\u8868 \u95ed\u5305\u8868\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u8868\uff0c\u5b83\u5b58\u50a8\u4e86\u6240\u6709\u8282\u70b9\u53ca\u5176\u5230\u6839\u8282\u70b9\u7684\u8ddd\u79bb\u3002\u4f7f\u7528\u95ed\u5305\u8868\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\u7684\u4e3b\u8981\u6b65\u9aa4\u5982\u4e0b\uff1a \u521b\u5efa\u6811\u8868\uff0c\u5b58\u50a8\u8282\u70b9\u53ca\u5176\u5230\u6839\u8282\u70b9\u7684\u8ddd\u79bb\u3002 \u6839\u636e name \u6a21\u7cca\u67e5\u8be2\u95ed\u5305\u8868\uff0c\u83b7\u53d6\u76f8\u5173\u8282\u70b9\u3002 \u6839\u636e parent \u548c distance \u5b57\u6bb5\uff0c\u5c06\u67e5\u8be2\u5230\u7684\u8282\u70b9\u7ec4\u5408\u4e3a\u6811\u7ed3\u6784\u3002 \u793a\u4f8b \u5047\u8bbe\u6709\u4e00\u4e2a\u6811\u72b6\u6570\u636e\u5982\u4e0b\uff1a &#8212;-\u98df\u7269 &#8212;- \u6c34\u679c &#8212;- \u9999\u8549 &#8212;- \u82f9\u679c &#8212;- \u8089\u7c7b &#8212;- \u9e21\u8089 &#8212;- \u80a5\u725b \u767b\u5f55\u540e\u590d\u5236 \u82e5\u8981\u67e5\u8be2\u5305\u542b &#8220;\u8089&#8221; \u7684\u6240\u6709\u8282\u70b9\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a select * from tree where self like &#8216;%\u8089%&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u67e5\u8be2\u7ed3\u679c\u5982\u4e0b\uff1a +&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+ | self | parent | distance [&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-6998","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6998","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=6998"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6998\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}