{"id":44671,"date":"2024-12-01T16:30:07","date_gmt":"2024-12-01T08:30:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44671\/"},"modified":"2024-12-01T16:30:07","modified_gmt":"2024-12-01T08:30:07","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-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/44671\/","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><b><\/b> <\/p>\n<h1>\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<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/173029649067223aaa4c938.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>mysql \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>\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>\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>\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>\u6700\u7ec8\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>{'\u98df\u7269': {'\u8089\u7c7b': {'\u9e21\u8089': {}}}}<\/pre>\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>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\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\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6570\u636e\u5e93\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\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 \u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~ mysql \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 \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;; \u67e5\u8be2\u7ed3\u679c\u5982\u4e0b\uff1a +&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+ | self | parent | [&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-44671","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44671","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=44671"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44671\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}