{"id":5322,"date":"2022-03-19T17:41:09","date_gmt":"2022-03-19T09:41:09","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5322"},"modified":"2022-03-19T17:41:09","modified_gmt":"2022-03-19T09:41:09","slug":"%e6%8e%a8%e8%8d%90%e4%b8%80%e7%a7%8d%e9%81%bf%e5%85%8d%e9%80%92%e5%bd%92%e6%9f%a5%e8%af%a2%e6%89%80%e6%9c%89%e5%ad%90%e9%83%a8%e9%97%a8%e7%9a%84%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5322","title":{"rendered":"\u63a8\u8350\u4e00\u79cd\u907f\u514d\u9012\u5f52\u67e5\u8be2\u6240\u6709\u5b50\u90e8\u95e8\u7684\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u4e0e\u5b9e\u73b0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u6211\u9605\u8bfb\u4e86\u4e00\u7bc7\u8001\u5916\u7684\u6587\u7ae0\uff0c\u91cc\u9762\u4ecb\u7ecd\u4e86\u901a\u8fc7\u4e00\u79cd\u5de7\u5999\u7684\u8bbe\u8ba1\uff0c\u5b9e\u73b0\u4e86\u9ad8\u6548\u7684\u90e8\u95e8\u6811\u67e5\u8be2\u8bbe\u8ba1\u3002\u907f\u514d\u9012\u5f52\u7b49\u4f4e\u6548\u67e5\u8be2\u3002\u4eca\u5929\u5206\u4eab\u63a8\u8350\u7ed9\u5927\u5bb6\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\u6811\u5f62\u7ed3\u6784\u7684\u5b58\u50a8\uff0c\u662f\u5728\u5b50\u8282\u70b9\u4e0a\u5b58\u50a8\u7236\u8282\u70b9\u7684\u7f16\u53f7\u6765\u786e\u5b9a\u5404\u8282\u70b9\u7684\u7236\u5b50\u5173\u7cfb\uff0c\u4f8b\u5982\u8fd9\u6837\u7684\u7ec4\u7ec7\u7ed3\u6784\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/dept0.png\" alt=\"\u9012\u5f52\u904d\u5386\u90e8\u95e8\u6811\" \/><figcaption>\u9012\u5f52\u904d\u5386\u90e8\u95e8\u6811<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u4e4b\u5bf9\u5e94\u7684\u8868\u6570\u636e(department)\uff1a<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>id<\/th><th>name<\/th><th>parent_id<\/th><th>level<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>\u8463\u4e8b\u957f<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>2<\/td><td>\u603b\u7ecf\u7406<\/td><td>1<\/td><td>2<\/td><\/tr><tr><td>3<\/td><td>\u4ea7\u54c1\u90e8<\/td><td>2<\/td><td>3<\/td><\/tr><tr><td>4<\/td><td>\u7814\u53d1\u90e8<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>5<\/td><td>\u8bbe\u8ba1\u90e8<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>6<\/td><td>\u884c\u653f\u603b\u76d1<\/td><td>2<\/td><td>3<\/td><\/tr><tr><td>7<\/td><td>\u8d22\u6838\u90e8<\/td><td>6<\/td><td>4<\/td><\/tr><tr><td>8<\/td><td>\u4f1a\u8ba1<\/td><td>7<\/td><td>5<\/td><\/tr><tr><td>9<\/td><td>\u51fa\u7eb3<\/td><td>7<\/td><td>5<\/td><\/tr><tr><td>10<\/td><td>\u884c\u653f\u90e8<\/td><td>6<\/td><td>4<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p class=\"wp-block-paragraph\">\u90e8\u95e8\u8868\u7ed3\u6784\uff08department\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id          \u90e8\u95e8\u7f16\u53f7\nname        \u90e8\u95e8\u540d\u79f0\nlevel       \u6240\u5728\u6811\u5c42\u7ea7\nparent_id   \u4e0a\u7ea7\u90e8\u95e8\u7f16\u53f7\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u95ee\u9898\u6765\u4e86<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u7684\u65b9\u5f0f\u5f88\u4e0d\u9519\uff0c\u53ef\u4ee5\u5f88\u76f4\u89c2\u7684\u4f53\u73b0\u5404\u4e2a\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u901a\u5e38\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u9700\u6c42\u3002\u4f46\u662f\u5f53\u4e1a\u52a1\u9700\u6c42\u53d8\u5f97\u591a\u4e86\uff0c\u6570\u636e\u91cf\u5e9e\u5927\u4e86\uff0c\u8fd9\u6837\u7684\u65b9\u5f0f\u5c31\u4e0d\u518d\u9002\u5408\u7528\u4e8e\u751f\u4ea7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1aPM \u52a0\u4e86\u4ee5\u4e0b\u9700\u6c42\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u67e5\u51fa\u6307\u5b9a\u90e8\u95e8\u4e0b\u6240\u6709\u5b50\u5b59\u90e8\u95e8\u3002<\/li><li>\u67e5\u8be2\u5b50\u5b59\u90e8\u95e8\u603b\u6570\u3002<\/li><li>\u5224\u65ad\u8282\u70b9\u662f\u5426\u53f6\u5b50\u8282\u70b9\u3002<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u67e5\u51fa\u6240\u6709\u5b50\u5b59\u90e8\u95e8<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u6307\u5b9a\u90e8\u95e8\u7f16\u53f7\uff0c\u4e00\u5c42\u4e00\u5c42\u4f7f\u7528<code>\u9012\u5f52<\/code>\u5f80\u4e0b\u67e5\uff0c\u53ef\u80fd\u662f\u591a\u6570\u4eba\u4f1a\u60f3\u5230\u7684\u65b9\u6cd5\u3002 \u5c3d\u7ba1\u5728 MySQL8.0 \u652f\u6301\u4e86 cte(\u516c\u5171\u8868\u8868\u8fbe\u5f0f)\uff0c\u9012\u5f52\u6548\u7387\u6bd4\u4f20\u7edf\u9012\u5f52\u65b9\u5f0f\u6709\u660e\u663e\u63d0\u5347\uff0c\u4f46\u662f\u67e5\u8be2\u6548\u7387\u4ecd\u4f1a\u968f\u7740\u90e8\u95e8\u6811\u5c42\u7ea7\u6df1\u5ea6\u7684\u63d0\u9ad8\u800c\u53d8\u5dee\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\uff0c\u4e00\u6b21\u6027\u67e5\u51fa\u6240\u6709\u6570\u636e\uff0c\u653e\u5165\u5185\u5b58\u4e2d\u5904\u7406\uff08\u6570\u636e\u91cf\u5c11\u65f6\uff0c\u53ef\u4ee5\u9009\u7528\u3002\u6570\u636e\u91cf\u591a\uff0c\u4e0d\u6015\u6328\u6253\u7684\u4eba\u4e5f\u53ef\u4ee5\u9009\u8fd9\u79cd\uff09~<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u67e5\u8be2\u5b50\u5b59\u90e8\u95e8\u603b\u6570<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9012\u5f52\u67e5\u8be2\u6bcf\u4e00\u5c42\u7684\u6570\u91cf\uff0c\u6700\u540e\u76f8\u52a0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5224\u65ad\u662f\u5426\u53f6\u5b50\u8282\u70b9<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u65b9\u6cd51\uff1a\u53ef\u4ee5\u52a0\u5b57\u6bb5<code>isLeaf<\/code>\u7684\u65b9\u5f0f\uff0c\u6765\u8868\u793a\u8fd9\u4e2a\u8282\u70b9\u662f\u5426\u662f\u53f6\u5b50\u8282\u70b9\u3002\n<\/li><li>\u65b9\u6cd52\uff1a\u76f4\u63a5\u901a\u8fc7\u67e5\u8be2<code>parent_id<\/code>=<code>\u5f53\u524did<\/code>\u7684 count \u662f\u5426\u5927\u4e8e 0\uff0c\u5927\u4e8e 0 \u8868\u793a\u4e0d\u662f\u53f6\u5b50\u8282\u70b9\uff0c\u7b49\u4e8e 0 \u8868\u793a\u4e3a\u53f6\u5b50\u8282\u70b9\u3002\n<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u65e5\u5e38\u4e2d\uff0c\u53ef\u80fd\u4f1a\u7ecf\u5e38\u4f7f\u7528\u4e0a\u8ff0\u7c7b\u4f3c\u65b9\u6cd5\u53bb\u89e3\u51b3\u7c7b\u4f3c\u7684\u95ee\u9898\uff0c\u4f46\u6211\u89c9\u5f97\u8fd9\u6837\u7684\u65b9\u6cd5\u5728\u6548\u7387\u4e0a\u4e0d\u662f\u6700\u4f18\u89e3\u3002\u4e8e\u662f\u4e4e\u5f00\u59cb\u67e5\u627e\u66f4\u597d\u7684\u65b9\u6848\u53bb\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8981\u4e0d\u8bd5\u8bd5\u8fd9\u4e2a\u65b9\u6cd5\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u5230\u540e\u9762\u67e5\u5230\u56fd\u5916\u4e00\u535a\u5ba2\u4e2d\uff0c\u89c1\u5230\u4e86\u6240\u8c13\u7684\u300a\u6539\u8fdb\u540e\u7684\u5148\u5e8f\u6811\u904d\u5386\u300b(<code>https:\/\/www.sitepoint.com\/hierarchical-data-database-2<\/code>)\u6587\u7ae0\uff08\u5929\u54ea\uff0c\u7adf\u7136\u662f\u4e00\u7bc7 2003 \u5e74\u53d1\u8868\u7684\u6587\u7ae0\uff09~<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ed6\u5177\u4f53\u662f\u600e\u4e48\u505a\u7684\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u662f\u56de\u5230\u521a\u521a\u7684\u7ec4\u7ec7\u67b6\u6784\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/dept1.png\" alt=\"\u590d\u6742\u7684\u90e8\u95e8\u7ec4\u7ec7\u67b6\u6784\" \/><figcaption>\u590d\u6742\u7684\u90e8\u95e8\u7ec4\u7ec7\u67b6\u6784<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u7ed9<code>\u8463\u4e8b\u957f<\/code>\u5de6\u503c\u8bbe\u4e3a<code>1<\/code>\uff0c\u4e0b\u7ea7\u90e8\u95e8<code>\u603b\u7ecf\u7406<\/code>\u5de6\u503c\u8bbe\u4e3a<code>2<\/code>\uff0c\u4ee5\u6b64\u7c7b\u63a8\u5730<code>\u6cbf\u7740\u8fb9\u7f18<\/code>\u5f00\u59cb\u904d\u5386\uff0c\u7ed9\u6bcf\u4e2a\u8282\u70b9\u52a0\u4e0a<code>\u5de6\u503c<\/code>\uff0c\u9047\u5230\u53f6\u5b50\u8282\u70b9\u5904\u7ed9\u8282\u70b9\u52a0\u4e0a<code>\u53f3\u503c<\/code>\uff0c\u518d\u7ee7\u7eed\u5411\u4e0a\u6cbf\u7740\u8fb9\u7f18\u7ee7\u7eed\u904d\u5386\uff0c\u904d\u5386\u7ed3\u675f\u56de\u5230\u6839\u8282\u70b9<code>\u53f3\u4fa7<\/code>\uff0c\u4f60\u5c06\u5f97\u5230\u7c7b\u4f3c\u8fd9\u6837\u7684\u7ed3\u6784\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/dept2.png\" alt=\"\u9ad8\u6548\u90e8\u95e8\u6811\u6570\u636e\u7ed3\u6784\" \/><figcaption>\u9ad8\u6548\u90e8\u95e8\u6811\u6570\u636e\u7ed3\u6784<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u904d\u5386\u5b8c\u540e\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u6709\u4e0e\u4e4b\u5bf9\u5e94\u7684<code>\u5de6\u53f3\u503c<\/code>\u3002 \u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u53bb\u9664<code>parent_id<\/code>\u5b57\u6bb5\uff0c\u6dfb\u52a0<code>lft<\/code>\uff0c<code>rgt<\/code>\uff0c\u6765\u5b58\u50a8\u5de6\u53f3\u503c\u3002<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>id<\/th><th>name<\/th><th>lft<\/th><th>rgt<\/th><th>level<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>\u8463\u4e8b\u957f<\/td><td>1<\/td><td>20<\/td><td>1<\/td><\/tr><tr><td>2<\/td><td>\u603b\u7ecf\u7406<\/td><td>2<\/td><td>19<\/td><td>2<\/td><\/tr><tr><td>3<\/td><td>\u4ea7\u54c1\u90e8<\/td><td>3<\/td><td>8<\/td><td>3<\/td><\/tr><tr><td>4<\/td><td>\u8bbe\u8ba1\u90e8<\/td><td>4<\/td><td>5<\/td><td>4<\/td><\/tr><tr><td>4<\/td><td>\u7814\u53d1\u90e8<\/td><td>6<\/td><td>7<\/td><td>4<\/td><\/tr><tr><td>6<\/td><td>\u884c\u653f\u603b\u76d1<\/td><td>9<\/td><td>18<\/td><td>3<\/td><\/tr><tr><td>7<\/td><td>\u8d22\u6838\u90e8<\/td><td>10<\/td><td>15<\/td><td>4<\/td><\/tr><tr><td>8<\/td><td>\u51fa\u7eb3<\/td><td>11<\/td><td>12<\/td><td>5<\/td><\/tr><tr><td>9<\/td><td>\u4f1a\u8ba1<\/td><td>13<\/td><td>14<\/td><td>5<\/td><\/tr><tr><td>10<\/td><td>\u884c\u653f\u90e8<\/td><td>16<\/td><td>17<\/td><td>4<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u548c\u7ed3\u6784\u51c6\u5907\u5b8c\u6bd5\uff0c\u6211\u4eec\u6765\u8bd5\u8bd5\u64cd\u4f5c\u89e3\u51b3\u4e0a\u9762\u7684\u9700\u6c42~<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u67e5\u51fa\u6240\u6709\u5b50\u5b59\u90e8\u95e8<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6839\u636e\u5f53\u524d\u8868\u7ed3\u6784\u7684\u89c4\u5f8b\uff0c\u53ef\u4ee5\u53d1\u73b0\uff0c\u8981\u60f3\u67e5\u51fa\u6240\u6709\u5b50\u5b59\u90e8\u95e8\uff0c\u53ea\u8981\u67e5\u5de6\u503c\u5728 <code>\u88ab\u67e5\u5bfb\u90e8\u95e8<\/code>\u7684<code>\u5de6\\\u53f3\u6570\u4e4b\u95f4<\/code>\u7684\u8282\u70b9\uff0c\u67e5\u51fa\u6765\u90fd\u662f\u4ed6\u7684\u5b50\u8282\u70b9\u3002 \u4f8b\u5982\uff1a\u67e5\u8be2<code>\u884c\u653f\u603b\u76d1<\/code>\u7684\u6240\u6709\u5b50\u90e8\u95e8\uff0c<code>\u884c\u653f\u603b\u76d1<\/code>\u7684\u5de6\u53f3\u6570\u662f<code>9<\/code>\u548c<code>18<\/code>\uff0c\u56e0\u6b64\u53ea\u9700\u8981\u7528<code>9<\/code>\u548c<code>18<\/code>\u505a<code>lft<\/code>\u5b57\u6bb5\u7684<code>between<\/code>\u67e5\u8be2\uff0c\u67e5\u8be2\u51fa\u7684\u7ed3\u679c\u5c31\u662f\u3010\u88ab\u67e5\u90e8\u95e8\u672c\u8eab\u6570\u636e\u548c\u6240\u6709\u5b50\u5b59\u90e8\u95e8\u3011\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @lft := 9;\nSET @rgt := 18;\nSELECT * FROM department WHERE lft BETWEEN @lft AND @rgt ORDER BY lft ASC;\n\/*\u4f8b\u5b50\u4e2d\u7528BETWEEN\u5c06\u88ab\u67e5\u90e8\u95e8\u672c\u8eab\u4e5f\u67e5\u4e86\u51fa\u6765\u3002\u5b9e\u9645\u4e2d\u53ef\u4ee5\u7528\u5927\u4e8e\u5c0f\u4e8e*\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b8c\u7f8e~<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u67e5\u8be2\u5b50\u5b59\u90e8\u95e8\u603b\u6570<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5230\u8fd9\u91cc\u53ef\u80fd\u4f1a\u8bf4\uff0c\u9700\u6c42 1 \u90fd\u89e3\u51b3\u4e86\uff0c\u67e5\u603b\u6570\u81ea\u7136\u4e5f\u5c31\u89e3\u51b3\u4e86\uff0c\u76f4\u63a5\u4e0a<code>select count<\/code>\u5c31\u53ef\u4ee5\u4e86\uff0c\u786e\u5b9e\u6ca1\u6709\u9519\uff0c\u4f46\u662f\u6ca1\u6709\u90a3\u4e2a\u5fc5\u8981\uff0c\u56e0\u4e3a\u6709\u4e2a\u7b80\u5355\u516c\u5f0f\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u516c\u5f0f\uff1a<code>\u603b\u6570 = (\u53f3\u503c - \u5de6\u503c - 1) \/ 2<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u884c\u653f\u603b\u76d1\u7684\u5b50\u5b59\u90e8\u95e8\u6570 = (18 - 9 - 1) \/ 2 = 4\n\n\u8463\u4e8b\u957f\u7684\u5b50\u5b59\u90e8\u95e8\u6570 = (20 - 1 - 1) \/ 2 = 9\n\n\u4f1a\u8ba1\u7684\u5b50\u90e8\u95e8\u6570 =  (14 - 13 - 1) \/ 2 = 0\n\n\u53ef\u4ee5\u6570\u6570\u770b\uff0c\u786e\u5b9e\u6ca1\u9519\u54e6\uff5e<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5224\u65ad\u662f\u5426\u53f6\u5b50\u8282\u70b9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u6709\u4e86\u4e0a\u8ff0\u8ba1\u7b97\u516c\u5f0f\u7b97\u603b\u6570\u7684\u7ecf\u9a8c\u540e\uff0c\u73b0\u5728\u5224\u65ad\u662f\u5426\u53f6\u5b50\u8282\u70b9\uff0c\u6709\u7684\u5c0f\u4f19\u4f34\u5df2\u7ecf\u77e5\u9053\u4e86\u600e\u4e48\u505a\uff0c\u90a3\u5c31\u662f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\u53f3\u503c - 1 == \u5de6\u503c<\/code>\u90a3\u4ed6\u5c31\u662f\u53f6\u5b50\u8282\u70b9\uff0c\u6216\u8005<code>\u5de6\u503c + 1 == \u53f3\u503c<\/code>\u90a3\u4ed6\u5c31\u662f\u53f6\u5b50\u8282\u70b9\uff0c\u53cd\u4e4b\u5219\u4e0d\u662f\u53f6\u5b50\u8282\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\u8bbe\u8ba1\u90e8<\/code>\uff0c<code>5 - 1 == 4<\/code>\uff0c\u56e0\u6b64\u4ed6\u662f\u53f6\u5b50\u8282\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\u8463\u4e8b\u957f<\/code>\uff0c<code>20 - 1 != 1<\/code>\uff0c\u56e0\u6b64\u4ed6\u4e0d\u662f\u53f6\u5b50\u8282\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81f3\u6b64\u5df2\u7ecf\u5b8c\u7f8e\u7684\u89e3\u51b3\u4e86\u4e0a\u8ff0\u9700\u6c42\u95ee\u9898\uff0c\u63a5\u4e0b\u6765\u518d\u5c1d\u8bd5\u4e00\u4e0b\u4e1a\u52a1\u7684\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5176\u4ed6\u57fa\u672c\u64cd\u4f5c<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b0\u589e\u90e8\u95e8<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u65b0\u589e\u4e00\u4e2a\u90e8\u95e8\u65f6\uff0c\u9700\u8981\u5bf9\u65b0\u589e\u8282\u70b9\u4f4d\u7f6e\u7684<code>\u540e\u7eed\u8fb9\u7f18<\/code>\u8fdb\u884c<code>\u52a02<\/code>\u64cd\u4f5c\uff0c\u56e0\u4e3a\u6bcf\u4e00\u4e2a\u8282\u70b9\u6709\u5de6\u53f3\u4e24\u4e2a\u6570\u503c\u3002\u8fd9\u4e2a\u64cd\u4f5c\u901a\u5e38\u9700\u8981\u653e\u5230<code>\u4e8b\u52a1<\/code>\u4e2d\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\u65b0\u589e\u6216\u5220\u9664\u8282\u70b9\u65f6\u53f3\u8282\u70b9\u5224\u65ad\u6761\u4ef6\u5e94\u8be5\u662f lft &gt;= \u5f53\u524d\u8282\u70b9\u7684 lft<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a\u5728\u7814\u53d1\u90e8\u95e8\u4e0b\u6dfb\u52a0\u4e00\u4e2a<code>\u65b0\u90e8\u95e8<\/code>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/dept3.png\" alt=\"\u65b0\u589e\u90e8\u95e8\" \/><figcaption>\u65b0\u589e\u90e8\u95e8<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u5e94 sql\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @lft := 7;\/*\u65b0\u90e8\u95e8\u7684\u5de6\u503c*\/\nSET @rgt := 8;\/*\u65b0\u90e8\u95e8\u7684\u5de6\u503c*\/\nSET @level := 5;\/*\u65b0\u90e8\u95e8\u7684\u5c42\u7ea7*\/\nbegin;\n\/*\u5c06\u63d2\u5165\u7684\u540e\u7eed\u8fb9\u7f18\u7684\u8282\u70b9\u5de6\u53f3\u6570+2*\/\nUPDATE department SET lft=lft+2 WHERE lft &gt; @lft;\nUPDATE department SET rgt=rgt+2 WHERE rgt &gt;= @lft;\n\/*\u63d2\u5165\u6570\u636e*\/\nINSERT INTO department(name,lft,rgt,level) VALUES('\u65b0\u90e8\u95e8',@lft,@rgt,level);\n\/*\u65b0\u589e\u5f71\u54cd\u884c\u6570\u4e3a0\u65f6\uff0c\u5fc5\u987b\u56de\u6eda*\/\ncommit;\n\/*rollback;*\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5220\u9664\u90e8\u95e8<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5220\u9664\u90e8\u95e8\u4e0e\u65b0\u589e\u90e8\u95e8\u7c7b\u4f3c\uff0c\u4e0d\u540c\u7684\u662f\u9700\u8981\u5bf9\u5220\u9664\u8282\u70b9\u7684<code>\u540e\u7eed\u8fb9\u7f18<\/code>\u8282\u70b9<code>\u51cf2<\/code>\u64cd\u4f5c\u3002 \u4f8b\u5982\uff1a\u5220\u9664\u521a\u521a\u6dfb\u52a0\u7684\u65b0\u90e8\u95e8\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/dept4.png\" alt=\"\u5220\u9664\u90e8\u95e8\" \/><figcaption>\u5220\u9664\u90e8\u95e8<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u5e94 sql\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @lft := 7;\/*\u8981\u5220\u9664\u7684\u8282\u70b9\u5de6\u503c*\/\nSET @rgt := 8;\/*\u8981\u5220\u9664\u7684\u8282\u70b9\u53f3\u503c*\/\nbegin;\nUPDATE department SET lft=lft-2 WHERE lft &gt; @lft;\nUPDATE department SET rgt=rgt-2 WHERE rgt &gt; @lft;\n\n\/*\u5220\u9664\u8282\u70b9*\/\nDELETE FROM department WHERE lft=@lft AND rgt=@rgt;\n\/*\u5220\u9664\u5f71\u54cd\u884c\u6570\u4e3a0\u65f6\uff0c\u5fc5\u987b\u56de\u6eda*\/\ncommit;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u67e5\u8be2\u76f4\u63a5\u5b50\u90e8\u95e8<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u67d0\u90e8\u95e8\u7684\u76f4\u63a5\u5b50\u90e8\u95e8\uff08\u5373\u4e0d\u5305\u542b\u5b59\u5b50\u90e8\u95e8\uff09\uff0c\u4f8b\u5982\uff1a\u67e5\u8be2<code>\u603b\u7ecf\u7406<\/code>\u4e0b\u7684\u76f4\u63a5\u5b50\u90e8\u95e8\u3002\u6b63\u5e38\u9700\u8981\u8fd4\u56de<code>\u4ea7\u54c1\u90e8<\/code>\u548c<code>\u884c\u653f\u603b\u76d1<\/code>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/dept5.png\" alt=\"\u67e5\u8be2\u76f4\u63a5\u5b50\u90e8\u95e8\" \/><figcaption>\u67e5\u8be2\u76f4\u63a5\u5b50\u90e8\u95e8<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u5e94\u7684 sql\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @level := 2;\/*\u603b\u7ecf\u7406\u7684level*\/\nSET @lft := 2;\/*\u603b\u7ecf\u7406\u7684\u5de6\u503c*\/\nSET @rgt := 19;\/*\u603b\u7ecf\u7406\u7684\u53f3\u503c*\/\n\nSELECT * FROM department WHERE lft &gt; @lft AND rgt &lt; @rgt AND level = @level+1;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u67e5\u8be2\u7956\u94fe\u8def\u5f84<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u67d0\u90e8\u95e8\u7684\u7956\u94fe\u8def\u5f84\u3002 \u4f8b\u5982\uff1a\u67e5\u8be2<code>\u4ea7\u54c1\u90e8<\/code>\u7684\u7956\u94fe\u8def\u5f84\uff0c\u6b63\u5e38\u9700\u8981\u8fd4\u56de<code>\u8463\u4e8b\u957f<\/code>,<code>\u603b\u7ecf\u7406<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @lft := 3;\/*\u4ea7\u54c1\u90e8\u5de6\u503c*\/\nSET @rgt := 8;\/*\u4ea7\u54c1\u90e8\u53f3\u503c*\/\n\nSELECT * FROM department WHERE lft &lt; @lft AND rgt &gt; @rgt ORDER BY lft ASC;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b8c\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u76ee\u524d\u5c31\u8fd9\u4e9b\uff0c\u6b22\u8fce\u6307\u6b63\u3001\u4ea4\u6d41\u3001\u8bc4\u8bba\u3002\u3002\u3002\u6211\u611f\u89c9\u8fd9\u4e2a\u90e8\u95e8\u8bbe\u8ba1\u5f88\u7ecf\u5178\uff0c\u975e\u5e38\u503c\u5f97\u6536\u85cf\uff0c\u4ee5\u4fbf\u7528\u65f6\u53ea\u9700\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.sitepoint.com\/hierarchical-data-database-2\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"https:\/\/www.sitepoint.com\/hierarchical-data-database-2\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\">https:\/\/www.sitepoint.com\/hierarchical-data-database-2<\/a><\/li><\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5322\">\u63a8\u8350\u4e00\u79cd\u907f\u514d\u9012\u5f52\u67e5\u8be2\u6240\u6709\u5b50\u90e8\u95e8\u7684\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u4e0e\u5b9e\u73b0<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6211\u9605\u8bfb\u4e86\u4e00\u7bc7\u8001\u5916\u7684\u6587\u7ae0\uff0c\u91cc\u9762\u4ecb\u7ecd\u4e86\u901a\u8fc7\u4e00\u79cd\u5de7\u5999\u7684\u8bbe\u8ba1\uff0c\u5b9e\u73b0\u4e86\u9ad8\u6548\u7684\u90e8\u95e8\u6811\u67e5\u8be2\u8bbe\u8ba1\u3002\u907f\u514d\u9012\u5f52\u7b49\u4f4e\u6548\u67e5\u8be2\u3002\u4eca\u5929 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-5322","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5322"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5322\/revisions"}],"predecessor-version":[{"id":5323,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5322\/revisions\/5323"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}