{"id":6905,"date":"2018-12-03T16:27:55","date_gmt":"2018-12-03T08:27:55","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6905"},"modified":"2018-12-03T16:38:17","modified_gmt":"2018-12-03T08:38:17","slug":"%e5%9f%ba%e4%ba%8eclosure-table%e5%ae%9e%e7%8e%b0%e5%88%86%e7%b1%bb%e7%9b%ae%e5%bd%95%e6%a0%91%e5%ad%98%e5%82%a8","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6905.html","title":{"rendered":"\u57fa\u4e8eClosure Table\u5b9e\u73b0\u5206\u7c7b\u76ee\u5f55\u6811\u5b58\u50a8"},"content":{"rendered":"<p>ClosureTable\u76f4\u8bd1\u8fc7\u6765\u53eb\u95ed\u5305\u8868\uff1f\u4e0d\u8fc7\u4e0d\u91cd\u8981\uff0cClosureTable\u4ee5\u4e00\u5f20\u8868\u5b58\u50a8\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4efb\u4f55\u4e24\u4e2a\u6709\u5173\u7cfb\uff08\u4e0a\u4e0b\u7ea7\uff09\u8282\u70b9\u7684\u5173\u8054\u4fe1\u606f<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/12\/closure-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6906\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/12\/closure-table.png\" alt=\"\" width=\"700\" height=\"320\" \/><\/a><\/p>\n<p>\u5b9a\u4e49\u5173\u7cfb\u8868CategoryTree\uff0c\u5176\u5305\u542b3\u4e2a\u5b57\u6bb5\uff1a<\/p>\n<ul>\n<li>ancestor \u7956\u5148\uff1a\u4e0a\u7ea7\u8282\u70b9\u7684id<\/li>\n<li>descendant \u5b50\u4ee3\uff1a\u4e0b\u7ea7\u8282\u70b9\u7684id<\/li>\n<li>distance \u8ddd\u79bb\uff1a\u5b50\u4ee3\u5230\u7956\u5148\u4e2d\u95f4\u9694\u4e86\u51e0\u7ea7<\/li>\n<\/ul>\n<p>\u8fd9\u4e09\u4e2a\u5b57\u6bb5\u7684\u7ec4\u5408\u662f\u552f\u4e00\u7684\uff0c\u56e0\u4e3a\u5728\u6811\u4e2d\uff0c\u4e00\u6761\u8def\u5f84\u53ef\u4ee5\u6807\u8bc6\u4e00\u4e2a\u8282\u70b9\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u628a\u5b83\u4eec\u7684\u7ec4\u5408\u4f5c\u4e3a\u4e3b\u952e\u3002\u4ee5\u56fe\u4e3a\u4f8b\uff0c\u8282\u70b96\u5230\u5b83\u4e0a\u4e00\u7ea7\u7684\u8282\u70b9(\u8282\u70b94)\u8ddd\u79bb\u4e3a1\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u4e3aancestor=4,descendant=6,distance=1\uff0c\u5230\u4e0a\u4e24\u7ea7\u7684\u8282\u70b9(\u8282\u70b91)\u8ddd\u79bb\u4e3a2,\u4e8e\u662f\u6709ancestor=1,descendant=6,distance=2\uff0c\u5230\u6839\u8282\u70b9\u7684\u8ddd\u79bb\u4e3a3\u4e5f\u662f\u5982\u6b64\uff0c\u6700\u540e\u8fd8\u8981\u5305\u542b\u4e00\u4e2a\u5230\u81ea\u5df1\u7684\u8fde\u63a5\uff0c\u5f53\u7136\u8ddd\u79bb\u5c31\u662f0\u4e86\u3002<\/p>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u4e0d\u5c3d\u8868\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7684\u8def\u5f84\u4fe1\u606f\uff0c\u8fd8\u5728\u5e26\u4e0a\u4e86\u8def\u5f84\u4e2d\u6bcf\u4e2a\u8282\u70b9\u7684\u4f4d\u7f6e\uff08\u8ddd\u79bb\uff09\uff0c\u5bf9\u4e8e\u6811\u7ed3\u6784\u5e38\u7528\u7684\u67e5\u8be2\u90fd\u80fd\u591f\u5f88\u65b9\u4fbf\u7684\u5904\u7406\u3002\u4e0b\u9762\u770b\u770b\u5982\u4f55\u7528\u7528\u5b83\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n<h2>\u5b50\u8282\u70b9\u67e5\u8be2<\/h2>\n<p>\u67e5\u8be2id\u4e3a5\u7684\u8282\u70b9\u7684\u76f4\u5c5e\u5b50\u8282\u70b9\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT descendant FROM CategoryTree WHERE ancestor=5 AND distance=1\r\n<\/pre>\n<p>\u67e5\u8be2\u6240\u6709\u5b50\u8282\u70b9\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT descendant FROM CategoryTree WHERE ancestor=5 AND distance&amp;amp;gt;0\r\n<\/pre>\n<p>\u67e5\u8be2\u67d0\u4e2a\u4e0a\u7ea7\u8282\u70b9\u7684\u5b50\u8282\u70b9\uff0c\u6362\u53e5\u8bdd\u8bf4\u5c31\u662f\u67e5\u8be2\u5177\u6709\u6307\u5b9a\u4e0a\u7ea7\u8282\u70b9\u7684\u8282\u70b9\uff0c\u4e5f\u5c31\u662fancestor\u5b57\u6bb5\u7b49\u4e8e\u4e0a\u7ea7\u8282\u70b9id\u5373\u53ef\uff0c\u7b2c\u4e8c\u4e2a\u8ddd\u79bbdistance\u51b3\u5b9a\u4e86\u67e5\u8be2\u7684\u5bf9\u8c61\u662f\u7531\u4e0a\u7ea7\u5f80\u4e0b\u90a3\u4e00\u5c42\u7684\uff0c\u7b49\u4e8e1\u5c31\u662f\u5f80\u4e0b\u4e00\u5c42\uff08\u76f4\u5c5e\u5b50\u8282\u70b9\uff09\uff0c\u5927\u4e8e0\u5c31\u662f\u6240\u6709\u5b50\u8282\u70b9\u3002\u8fd9\u4e24\u4e2a\u67e5\u8be2\u90fd\u662f\u4e00\u53e5\u5b8c\u6210\u3002<\/p>\n<h2>\u8def\u5f84\u67e5\u8be2<\/h2>\n<p>\u67e5\u8be2\u7531\u6839\u8282\u70b9\u5230id\u4e3a10\u7684\u8282\u70b9\u4e4b\u95f4\u7684\u6240\u6709\u8282\u70b9\uff0c\u6309\u7167\u5c42\u7ea7\u7531\u5c0f\u5230\u5927\u6392\u5e8f<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT ancestor FROM CategoryTree WHERE descendant=10 ORDER BY distance DESC\r\n<\/pre>\n<p>\u67e5\u8be2id\u4e3a10\u7684\u8282\u70b9\uff08\u542b\uff09\u5230id\u4e3a3\u7684\u8282\u70b9\uff08\u4e0d\u542b\uff09\u4e4b\u95f4\u7684\u6240\u6709\u8282\u70b9\uff0c\u6309\u7167\u5c42\u7ea7\u7531\u5c0f\u5230\u5927\u6392\u5e8f<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT ancestor FROM CategoryTree WHERE descendant=10 AND\r\ndistance &lt; (SELECT distance FROM CategoryTree WHERE descendant=10 AND ancestor=3)\r\nORDER BY distance DESC\r\n<\/pre>\n<p>\u67e5\u8be2\u8def\u5f84\uff0c\u53ea\u9700\u8981\u77e5\u9053descendant\u5373\u53ef\uff0c\u56e0\u4e3adescendant\u5b57\u6bb5\u6240\u5728\u7684\u884c\u5c31\u662f\u8bb0\u5f55\u8fd9\u4e2a\u8282\u70b9\u4e0e\u5176\u4e0a\u7ea7\u8282\u70b9\u7684\u5173\u7cfb\u3002\u5982\u679c\u8981\u8fc7\u6ee4\u6389\u4e00\u4e9b\u5219\u53ef\u4ee5\u9650\u5236distance\u7684\u503c\u3002<\/p>\n<h2>\u67e5\u8be2\u8282\u70b9\u6240\u5728\u7684\u5c42\u7ea7\uff08\u6df1\u5ea6\uff09<\/h2>\n<p>\u67e5\u8be2id\u4e3a5\u7684\u8282\u70b9\u662f\u7b2c\u51e0\u7ea7\u7684<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT distance FROM CategoryTree WHERE descendant=5 AND ancestor=0\r\n<\/pre>\n<p>\u67e5\u8be2id\u4e3a5\u7684\u8282\u70b9\u662fid\u4e3a10\u7684\u8282\u70b9\u5f80\u4e0b\u7b2c\u51e0\u7ea7<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT distance FROM CategoryTree WHERE descendant=5 AND ancestor=10\r\n<\/pre>\n<p>\u67e5\u8be2\u5c42\u7ea7\uff08\u6df1\u5ea6\uff09\u975e\u5e38\u7b80\u5355\uff0c\u56e0\u4e3adistance\u5b57\u6bb5\u5c31\u662f\u3002\u76f4\u63a5\u4ee5\u4e0a\u4e0b\u7ea7\u7684\u8282\u70b9id\u4e3a\u6761\u4ef6\uff0c\u67e5\u8be2\u8ddd\u79bb\u5373\u53ef\u3002<\/p>\n<h2>\u67e5\u8be2\u67d0\u4e00\u5c42\u7684\u6240\u6709\u8282\u70b9<\/h2>\n<p>\u67e5\u8be2\u6240\u6709\u7b2c\u4e09\u5c42\u7684\u8282\u70b9<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT descendant FROM CategoryTree WHERE ancestor=0 AND distance=3\r\n<\/pre>\n<p>\u8fd9\u4e2a\u5c31\u4e0d\u8be6\u7ec6\u8bf4\u4e86\uff0c\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<h2>\u63d2\u5165<\/h2>\n<p>\u63d2\u5165\u548c\u79fb\u52a8\u5c31\u4e0d\u662f\u90a3\u4e48\u65b9\u4fbf\u4e86\uff0c\u5f53\u4e00\u4e2a\u8282\u70b9\u63d2\u5165\u5230\u67d0\u4e2a\u7236\u8282\u70b9\u4e0b\u65b9\u65f6\uff0c\u5b83\u5c06\u5177\u6709\u4e0e\u7236\u8282\u70b9\u76f8\u4f3c\u7684\u8def\u5f84\uff0c\u7136\u540e\u518d\u52a0\u4e0a\u4e00\u4e2a\u81ea\u8eab\u8fde\u63a5\u5373\u53ef\u3002<\/p>\n<p>\u6240\u4ee5\u63d2\u5165\u64cd\u4f5c\u9700\u8981\u4e24\u6761\u8bed\u53e5\uff0c\u7b2c\u4e00\u6761\u590d\u5236\u7236\u8282\u70b9\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5e76\u628a\u8fd9\u4e9b\u8bb0\u5f55\u7684distance\u52a0\u4e00\uff0c\u56e0\u4e3a\u5b50\u8282\u70b9\u5230\u6bcf\u4e2a\u4e0a\u7ea7\u8282\u70b9\u7684\u8ddd\u79bb\u90fd\u6bd4\u5b83\u7684\u7236\u8282\u70b9\u591a\u4e00\u3002\u5f53\u7136descendant\u4e5f\u8981\u6539\u6210\u81ea\u5df1\u7684\u3002<\/p>\n<p>\u4f8b\u5982\u628aid\u4e3a10\u7684\u8282\u70b9\u63d2\u5165\u5230id\u4e3a5\u7684\u8282\u70b9\u4e0b\u65b9\uff08\u8fd9\u91cc\u7528\u4e86MySQL\u7684\u8bed\u8a00\uff09<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nINSERT INTO CategoryTree(ancestor,descendant,distance) (SELECT ancestor,10,distance+1 FROM CategoryTree WHERE descendant=5)\r\n<\/pre>\n<p>\u7136\u540e\u5c31\u662f\u52a0\u5165\u81ea\u8eab\u8fde\u63a5\u7684\u8bb0\u5f55\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nINSERT INTO CategoryTree(ancestor,descendant,distance) VALUES(10,10,0)\r\n<\/pre>\n<h2>\u79fb\u52a8<\/h2>\n<p>\u8282\u70b9\u7684\u79fb\u52a8\u6ca1\u6709\u5f88\u597d\u7684\u89e3\u51b3\u65b9\u6cd5\uff0c\u56e0\u4e3a\u65b0\u4f4d\u7f6e\u6240\u5728\u7684\u6df1\u5ea6\u3001\u8def\u5f84\u90fd\u53ef\u80fd\u4e0d\u4e00\u6837\uff0c\u8fd9\u5c31\u5bfc\u81f4\u79fb\u52a8\u64cd\u4f5c\u4e0d\u662f\u4ec5\u9760UPDATE\u8bed\u53e5\u80fd\u5b8c\u6210\u7684\uff0c\u8fd9\u91cc\u9009\u62e9\u5220\u9664+\u63d2\u5165\u5b9e\u73b0\u79fb\u52a8\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5728\u6709\u5b50\u6811\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0a\u7ea7\u8282\u70b9\u7684\u79fb\u52a8\u8fd8\u5c06\u5bfc\u81f4\u4e0b\u7ea7\u8282\u70b9\u7684\u8def\u5f84\u6539\u53d8\uff0c\u6240\u4ee5\u79fb\u52a8\u4e0a\u7ea7\u8282\u70b9\u4e4b\u540e\u8fd8\u9700\u8981\u4fee\u590d\u4e0b\u7ea7\u8282\u70b9\u7684\u8bb0\u5f55\uff0c\u8fd9\u5c31\u9700\u8981\u9012\u5f52\u6240\u6709\u4e0b\u7ea7\u8282\u70b9\u3002<\/p>\n<p>\u5220\u9664id=5\u8282\u70b9\u7684\u6240\u6709\u8bb0\u5f55<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nDELETE FROM CategoryTree WHERE descendant=5\r\n<\/pre>\n<p>\u7136\u540e\u914d\u5408\u4e0a\u9762\u4e00\u8282\u7684\u63d2\u5165\u64cd\u4f5c\u5b9e\u73b0\u79fb\u52a8\u3002<\/p>\n<h2>\u53c2\u8003\u8d44\u6599\uff1a<\/h2>\n<ul>\n<li><a href=\"https:\/\/segmentfault.com\/a\/1190000014284076\">\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u4e00\u68f5\u6811\uff0c\u5b9e\u73b0\u65e0\u9650\u7ea7\u5206\u7c7b<\/a><\/li>\n<li><a href=\"https:\/\/coderwall.com\/p\/lixing\/closure-tables-for-browsing-trees-in-sql\">Closure Tables for Browsing Trees in SQL<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/16069840\/rendering-a-tree-from-a-closure-table-select-statement\">Rendering a tree from a closure table SELECT statement?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>ClosureTable\u76f4\u8bd1\u8fc7\u6765\u53eb\u95ed\u5305\u8868\uff1f\u4e0d\u8fc7\u4e0d\u91cd\u8981\uff0cClosureTable\u4ee5\u4e00\u5f20\u8868\u5b58\u50a8\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5176\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6905","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6905"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6905\/revisions"}],"predecessor-version":[{"id":6909,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6905\/revisions\/6909"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}