{"id":5862,"date":"2024-11-14T10:26:50","date_gmt":"2024-11-14T02:26:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5862\/"},"modified":"2024-11-14T10:26:50","modified_gmt":"2024-11-14T02:26:50","slug":"sql%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e6%9f%a5%e9%80%92%e5%bd%92","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5862\/","title":{"rendered":"sql\u8bed\u53e5\u600e\u4e48\u67e5\u9012\u5f52"},"content":{"rendered":"<blockquote><p>\n  sql \u901a\u8fc7\u9012\u5f52\u67e5\u8be2\u5141\u8bb8\u67e5\u8be2\u5177\u6709\u81ea\u5f15\u7528\u5173\u7cfb\u7684\u6570\u636e\u3002\u6b65\u9aa4\u5305\u62ec\uff1a\u786e\u5b9a\u9012\u5f52\u5173\u7cfb\u3002\u7f16\u5199\u9012\u5f52 cte\uff0c\u4f7f\u7528 union all \u8fde\u63a5\u9012\u5f52\u5b50\u67e5\u8be2\u548c\u57fa\u672c\u67e5\u8be2\u3002\u6307\u5b9a\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u3002\u4f7f\u7528 with \u5b50\u53e5\u5b9a\u4e49 cte\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321061945297.jpg\" class=\"aligncenter\" title=\"sql\u8bed\u53e5\u600e\u4e48\u67e5\u9012\u5f52\u63d2\u56fe\" alt=\"sql\u8bed\u53e5\u600e\u4e48\u67e5\u9012\u5f52\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u8bed\u53e5\u5982\u4f55\u67e5\u8be2\u9012\u5f52<\/strong><\/p>\n<p>SQL\uff08\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\uff09\u63d0\u4f9b\u4e86\u67e5\u8be2\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u4e2d\u6570\u636e\u7684\u5f3a\u5927\u65b9\u6cd5\uff0c\u5305\u62ec\u9012\u5f52\u67e5\u8be2\u3002\u9012\u5f52\u67e5\u8be2\u5141\u8bb8\u60a8\u4ece\u8868\u4e2d\u68c0\u7d22\u6570\u636e\uff0c\u540c\u65f6\u8003\u8651\u4e0e\u540c\u4e00\u8868\u81ea\u8eab\u7684\u5173\u7cfb\u3002<\/p>\n<p><strong>\u4f7f\u7528\u9012\u5f52\u67e5\u8be2\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u9012\u5f52\u5173\u7cfb\uff1a<\/strong>\u8bc6\u522b\u8868\u4e2d\u5177\u6709\u81ea\u5f15\u7528\u5173\u7cfb\u7684\u5217\u3002\u8fd9\u662f\u8868\u793a\u9012\u5f52\u7684\u57fa\u7840\u3002<\/li>\n<li> <strong>\u7f16\u5199\u9012\u5f52 CTE\uff1a<\/strong>\u521b\u5efa\u516c\u5171\u8868\u8868\u8fbe\u5f0f (CTE)\uff0c\u4f7f\u7528 UNION ALL \u8fde\u63a5\u9012\u5f52\u5b50\u67e5\u8be2\u548c\u57fa\u672c\u67e5\u8be2\u3002\u9012\u5f52\u5b50\u67e5\u8be2\u5f15\u7528\u8868 itself \u4ee5\u67e5\u627e\u9012\u5f52\u5173\u7cfb\u3002<\/li>\n<li> <strong>\u6307\u5b9a\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff1a<\/strong>\u5728\u9012\u5f52\u5b50\u67e5\u8be2\u4e2d\u6307\u5b9a\u4e00\u4e2a\u6761\u4ef6\uff0c\u5f53\u6ee1\u8db3\u65f6\uff0c\u5c06\u505c\u6b62\u9012\u5f52\u8fc7\u7a0b\u3002<\/li>\n<li> <strong>\u4f7f\u7528 WITH \u5b50\u53e5\uff1a<\/strong>\u5c06 CTE \u5b9a\u4e49\u4e3a WITH \u5b50\u53e5\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a Employee \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b EmployeeID\u3001ReportsTo\uff08\u6307\u5411\u7ecf\u7406 ID \u7684\u5916\u952e\uff09\u548c Name \u5217\u3002\u8981\u67e5\u627e\u76f4\u63a5\u548c\u95f4\u63a5\u62a5\u544a\u7ed9\u7ecf\u7406 10 \u7684\u6240\u6709\u5458\u5de5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u9012\u5f52 CTE\uff1a<\/p>\n<pre>WITH EmployeeHierarchy AS (\n    SELECT EmployeeID, ReportsTo, Name\n    FROM Employee\n    WHERE EmployeeID = 10\n    UNION ALL\n    SELECT E.EmployeeID, E.ReportsTo, E.Name\n    FROM Employee E\n    JOIN EmployeeHierarchy EH ON E.ReportsTo = EH.EmployeeID\n)\nSELECT *\nFROM EmployeeHierarchy;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u6240\u6709\u76f4\u63a5\u6216\u95f4\u63a5\u62a5\u544a\u7ed9\u7ecf\u7406 10 \u7684\u5458\u5de5\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u9012\u5f52\u67e5\u8be2\u5141\u8bb8\u60a8\u5bf9\u5177\u6709\u5c42\u6b21\u7ed3\u6784\u6216\u81ea\u5f15\u7528\u5173\u7cfb\u7684\u6570\u636e\u8fdb\u884c\u590d\u6742\u67e5\u8be2\u3002<\/li>\n<li>\u5b83\u4eec\u5bf9\u4e8e\u67e5\u627e\u73af\u8def\u548c\u4f9d\u8d56\u5173\u7cfb\u975e\u5e38\u6709\u7528\u3002<\/li>\n<li>\u5b83\u4eec\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u67e5\u8be2\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u67e5\u9012\u5f52\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>sql \u901a\u8fc7\u9012\u5f52\u67e5\u8be2\u5141\u8bb8\u67e5\u8be2\u5177\u6709\u81ea\u5f15\u7528\u5173\u7cfb\u7684\u6570\u636e\u3002\u6b65\u9aa4\u5305\u62ec\uff1a\u786e\u5b9a\u9012\u5f52\u5173\u7cfb\u3002\u7f16\u5199\u9012\u5f52 cte\uff0c\u4f7f\u7528 union all \u8fde\u63a5\u9012\u5f52\u5b50\u67e5\u8be2\u548c\u57fa\u672c\u67e5\u8be2\u3002\u6307\u5b9a\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\u3002\u4f7f\u7528 with \u5b50\u53e5\u5b9a\u4e49 cte\u3002 SQL \u8bed\u53e5\u5982\u4f55\u67e5\u8be2\u9012\u5f52 SQL\uff08\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\uff09\u63d0\u4f9b\u4e86\u67e5\u8be2\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u4e2d\u6570\u636e\u7684\u5f3a\u5927\u65b9\u6cd5\uff0c\u5305\u62ec\u9012\u5f52\u67e5\u8be2\u3002\u9012\u5f52\u67e5\u8be2\u5141\u8bb8\u60a8\u4ece\u8868\u4e2d\u68c0\u7d22\u6570\u636e\uff0c\u540c\u65f6\u8003\u8651\u4e0e\u540c\u4e00\u8868\u81ea\u8eab\u7684\u5173\u7cfb\u3002 \u4f7f\u7528\u9012\u5f52\u67e5\u8be2\u7684\u6b65\u9aa4\uff1a \u786e\u5b9a\u9012\u5f52\u5173\u7cfb\uff1a\u8bc6\u522b\u8868\u4e2d\u5177\u6709\u81ea\u5f15\u7528\u5173\u7cfb\u7684\u5217\u3002\u8fd9\u662f\u8868\u793a\u9012\u5f52\u7684\u57fa\u7840\u3002 \u7f16\u5199\u9012\u5f52 CTE\uff1a\u521b\u5efa\u516c\u5171\u8868\u8868\u8fbe\u5f0f (CTE)\uff0c\u4f7f\u7528 UNION ALL \u8fde\u63a5\u9012\u5f52\u5b50\u67e5\u8be2\u548c\u57fa\u672c\u67e5\u8be2\u3002\u9012\u5f52\u5b50\u67e5\u8be2\u5f15\u7528\u8868 itself \u4ee5\u67e5\u627e\u9012\u5f52\u5173\u7cfb\u3002 \u6307\u5b9a\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff1a\u5728\u9012\u5f52\u5b50\u67e5\u8be2\u4e2d\u6307\u5b9a\u4e00\u4e2a\u6761\u4ef6\uff0c\u5f53\u6ee1\u8db3\u65f6\uff0c\u5c06\u505c\u6b62\u9012\u5f52\u8fc7\u7a0b\u3002 \u4f7f\u7528 WITH \u5b50\u53e5\uff1a\u5c06 CTE \u5b9a\u4e49\u4e3a WITH \u5b50\u53e5\u7684\u4e00\u90e8\u5206\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a Employee \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b EmployeeID\u3001ReportsTo\uff08\u6307\u5411\u7ecf\u7406 ID \u7684\u5916\u952e\uff09\u548c Name \u5217\u3002\u8981\u67e5\u627e\u76f4\u63a5\u548c\u95f4\u63a5\u62a5\u544a\u7ed9\u7ecf\u7406 10 \u7684\u6240\u6709\u5458\u5de5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u9012\u5f52 CTE\uff1a WITH EmployeeHierarchy AS ( SELECT EmployeeID, ReportsTo, Name FROM Employee WHERE EmployeeID = [&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-5862","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5862","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=5862"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5862\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}