{"id":7181,"date":"2024-11-14T14:43:49","date_gmt":"2024-11-14T06:43:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7181\/"},"modified":"2024-11-14T14:43:49","modified_gmt":"2024-11-14T06:43:49","slug":"%e4%bd%bf%e7%94%a8%e9%97%ad%e5%8c%85%e8%a1%a8%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%9c%b0%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2%e6%a0%91%e7%8a%b6%e7%bb%93%e6%9e%84%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7181\/","title":{"rendered":"\u4f7f\u7528\u95ed\u5305\u8868\u5982\u4f55\u9ad8\u6548\u5730\u6a21\u7cca\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173059650432561.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528\u95ed\u5305\u8868\u5982\u4f55\u9ad8\u6548\u5730\u6a21\u7cca\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528\u95ed\u5305\u8868\u5982\u4f55\u9ad8\u6548\u5730\u6a21\u7cca\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528\u95ed\u5305\u8868\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u9700\u8981\u6839\u636e\u6811\u72b6\u8868\u7ed3\u6784\u6a21\u7cca\u67e5\u8be2\u6570\u636e\uff0c\u4fdd\u7559\u7b26\u5408\u6761\u4ef6\u7684\u5c42\u7ea7\uff0c\u4f8b\u5982\uff1a\u6709\u8868\u5b57\u6bb5\u4e3a id\u3001parent_id \u548c name\uff0c\u60f3\u8981\u6839\u636e name \u6a21\u7cca\u67e5\u8be2\u51fa &#8220;%\u8089%&#8221; \u76f8\u5173\u7684\u5c42\u7ea7\uff0c\u5e76\u53ea\u663e\u793a\u67e5\u8be2\u7ed3\u679c\u7684\u76f8\u5173\u5c42\u7ea7\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u5efa\u8bae\u4f7f\u7528\u95ed\u5305\u8868\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a tree \u7684\u8868\uff0c\u5b83\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<\/p>\n<ul>\n<li>self\uff1a\u5f53\u524d\u8282\u70b9\u7684 id\u3002<\/li>\n<li>parent\uff1a\u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9 id\u3002<\/li>\n<li>distance\uff1a\u5f53\u524d\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8ddd\u79bb\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u6811\u4ee5\u95ed\u5305\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8868\u4e2d\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\u751f\u6210\u95ed\u5305\u8868\uff1a<\/li>\n<\/ol>\n<pre>create table tree(self int, parent int, distance int);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5c06\u6811\u72b6\u6570\u636e\u63d2\u5165\u95ed\u5305\u8868\u4e2d\uff1a<\/li>\n<\/ol>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u6811\u7ed3\u6784\u9010\u884c\u63d2\u5165\u6570\u636e\u3002<\/p>\n<ol>\n<li>\u6839\u636e\u6761\u4ef6\u67e5\u8be2\u6570\u636e\uff1a<\/li>\n<\/ol>\n<pre>select * from tree where self like '%\u8089%';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728\u4ee3\u7801\u4e2d\u6309 self \u5206\u7c7b\u5e76\u5408\u5e76\u6811\uff1a<\/li>\n<\/ol>\n<pre>from 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><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u4e0d\u8981\u76f4\u63a5\u6620\u5c04\u4e2d\u6587\uff0c\u5e94\u8be5\u4e3a\u6bcf\u4e2a\u5206\u7c7b\u5206\u914d\u4e00\u4e2a id\u3002<\/li>\n<li>\u95ed\u5305\u8868\u7684\u5b58\u50a8\u7a7a\u95f4\u4f1a\u968f\u7740\u6811\u7684\u5c42\u7ea7\u6df1\u5ea6\u6210\u500d\u589e\u52a0\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fd9\u5e76\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<li>\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u90fd\u9700\u8981\u91cd\u5199\uff0c\u5de5\u4f5c\u91cf\u8f83\u5927\uff0c\u4f46\u8fd9\u79cd\u65b9\u6cd5\u662f\u4e00\u79cd\u76f8\u5bf9\u89c4\u8303\u7684\u505a\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528\u95ed\u5305\u8868\u5982\u4f55\u9ad8\u6548\u5730\u6a21\u7cca\u67e5\u8be2\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>\u4f7f\u7528\u95ed\u5305\u8868\u67e5\u8be2\u6811\u72b6\u7ed3\u6784\u6570\u636e \u95ee\u9898\uff1a \u9700\u8981\u6839\u636e\u6811\u72b6\u8868\u7ed3\u6784\u6a21\u7cca\u67e5\u8be2\u6570\u636e\uff0c\u4fdd\u7559\u7b26\u5408\u6761\u4ef6\u7684\u5c42\u7ea7\uff0c\u4f8b\u5982\uff1a\u6709\u8868\u5b57\u6bb5\u4e3a id\u3001parent_id \u548c name\uff0c\u60f3\u8981\u6839\u636e name \u6a21\u7cca\u67e5\u8be2\u51fa &#8220;%\u8089%&#8221; \u76f8\u5173\u7684\u5c42\u7ea7\uff0c\u5e76\u53ea\u663e\u793a\u67e5\u8be2\u7ed3\u679c\u7684\u76f8\u5173\u5c42\u7ea7\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u5efa\u8bae\u4f7f\u7528\u95ed\u5305\u8868\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a tree \u7684\u8868\uff0c\u5b83\u5305\u542b\u4ee5\u4e0b\u5217\uff1a self\uff1a\u5f53\u524d\u8282\u70b9\u7684 id\u3002 parent\uff1a\u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9 id\u3002 distance\uff1a\u5f53\u524d\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8ddd\u79bb\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u6811\u4ee5\u95ed\u5305\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8868\u4e2d\u3002 \u6b65\u9aa4\uff1a \u751f\u6210\u95ed\u5305\u8868\uff1a create table tree(self int, parent int, distance int); \u767b\u5f55\u540e\u590d\u5236 \u5c06\u6811\u72b6\u6570\u636e\u63d2\u5165\u95ed\u5305\u8868\u4e2d\uff1a \u4f60\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u6811\u7ed3\u6784\u9010\u884c\u63d2\u5165\u6570\u636e\u3002 \u6839\u636e\u6761\u4ef6\u67e5\u8be2\u6570\u636e\uff1a select * from tree where self like &#8216;%\u8089%&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u5728\u4ee3\u7801\u4e2d\u6309 self \u5206\u7c7b\u5e76\u5408\u5e76\u6811\uff1a from itertools import groupby root = {} for _, [&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-7181","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7181","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=7181"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7181\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}