{"id":217803,"date":"2023-12-23T00:31:33","date_gmt":"2023-12-22T16:31:33","guid":{"rendered":"https:\/\/www.idc.net\/help\/217803\/"},"modified":"2023-12-23T00:31:33","modified_gmt":"2023-12-22T16:31:33","slug":"mysql%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8self-join%e5%b0%86%e8%a1%a8%e8%87%aa%e8%ba%ab%e4%bd%9c%e4%b8%ba%e4%b8%a4%e4%b8%aa%e8%a1%a8%e8%bf%9b%e8%a1%8c%e8%87%aa%e8%bf%9e","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/217803\/","title":{"rendered":"MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528SELF JOIN\u5c06\u8868\u81ea\u8eab\u4f5c\u4e3a\u4e24\u4e2a\u8868\u8fdb\u884c\u81ea\u8fde\u63a5"},"content":{"rendered":"<p><title id=\"mysql%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8self-join%e5%b0%86%e8%a1%a8%e8%87%aa%e8%ba%ab%e4%bd%9c%e4%b8%ba%e4%b8%a4%e4%b8%aa%e8%a1%a8%e8%bf%9b%e8%a1%8c%e8%87%aa%e8%bf%9e-IlgMFuhHxl\">MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528SELF JOIN\u5c06\u8868\u81ea\u8eab\u4f5c\u4e3a\u4e24\u4e2a\u8868\u8fdb\u884c\u81ea\u8fde\u63a5<\/title><\/p>\n<h1 id=\"mysql%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8self-join%e5%b0%86%e8%a1%a8%e8%87%aa%e8%ba%ab%e4%bd%9c%e4%b8%ba%e4%b8%a4%e4%b8%aa%e8%a1%a8%e8%bf%9b%e8%a1%8c%e8%87%aa%e8%bf%9e-IlgMFuhHxl\">MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528SELF JOIN\u5c06\u8868\u81ea\u8eab\u4f5c\u4e3a\u4e24\u4e2a\u8868\u8fdb\u884c\u81ea\u8fde\u63a5<\/h1>\n<p>\u5728MySQL\u4e2d\uff0cSELF JOIN\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fde\u63a5\u64cd\u4f5c\uff0c\u5b83\u5141\u8bb8\u5c06\u8868\u81ea\u8eab\u4f5c\u4e3a\u4e24\u4e2a\u8868\u8fdb\u884c\u8fde\u63a5\u3002\u8fd9\u79cd\u8fde\u63a5\u64cd\u4f5c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5f53\u6211\u4eec\u9700\u8981\u5728\u540c\u4e00\u5f20\u8868\u4e2d\u6bd4\u8f83\u4e0d\u540c\u884c\u7684\u6570\u636e\u65f6\u3002<\/p>\n<h2 id=\"self-join%e7%9a%84%e8%af%ad%e6%b3%95-IlgMFuhHxl\">SELF JOIN\u7684\u8bed\u6cd5<\/h2>\n<p>SELF JOIN\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column_name(s)\nFROM table1 T1, table1 T2\nWHERE condition;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u8bed\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e24\u4e2a\u8868\u522b\u540dT1\u548cT2\u6765\u8868\u793a\u540c\u4e00\u5f20\u8868\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728WHERE\u5b50\u53e5\u4e2d\u4f7f\u7528\u8fd9\u4e24\u4e2a\u8868\u522b\u540d\u6765\u6bd4\u8f83\u4e0d\u540c\u884c\u7684\u6570\u636e\u3002<\/p>\n<h2 id=\"self-join%e7%9a%84%e7%a4%ba%e4%be%8b-IlgMFuhHxl\">SELF JOIN\u7684\u793a\u4f8b<\/h2>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u4f7f\u7528SELF JOIN\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a\"employees\"\u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u5458\u5de5\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u5458\u5de5ID\u3001\u59d3\u540d\u548c\u7ecf\u7406ID\u3002\u6211\u4eec\u60f3\u8981\u627e\u51fa\u6bcf\u4e2a\u5458\u5de5\u7684\u7ecf\u7406\u59d3\u540d\u3002<\/p>\n<pre><code>CREATE TABLE employees (\n  employee_id INT PRIMARY KEY,\n  employee_name VARCHAR(50),\n  manager_id INT\n);\n\nINSERT INTO employees (employee_id, employee_name, manager_id)\nVALUES (1, 'John', 3),\n       (2, 'Jane', 3),\n       (3, 'Mike', 4),\n       (4, 'Tom', NULL),\n       (5, 'Lisa', 4);<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SELF JOIN\u6765\u627e\u51fa\u6bcf\u4e2a\u5458\u5de5\u7684\u7ecf\u7406\u59d3\u540d\uff1a<\/p>\n<pre><code>SELECT e.employee_name AS employee, m.employee_name AS manager\nFROM employees e\nJOIN employees m ON e.manager_id = m.employee_id;<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee5\u4e0a\u67e5\u8be2\uff0c\u6211\u4eec\u5c06\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<table>\n<tr>\n<th>employee<\/th>\n<th>manager<\/th>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>Mike<\/td>\n<\/tr>\n<tr>\n<td>Jane<\/td>\n<td>Mike<\/td>\n<\/tr>\n<tr>\n<td>Mike<\/td>\n<td>Tom<\/td>\n<\/tr>\n<tr>\n<td>Lisa<\/td>\n<td>Tom<\/td>\n<\/tr>\n<\/table>\n<p>\u4ece\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u6bcf\u4e2a\u5458\u5de5\u7684\u7ecf\u7406\u59d3\u540d\u90fd\u88ab\u6b63\u786e\u5730\u663e\u793a\u51fa\u6765\u3002<\/p>\n<h2 id=\"%e6%80%bb%e7%bb%93-IlgMFuhHxl\">\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u4f7f\u7528SELF JOIN\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8868\u81ea\u8eab\u4f5c\u4e3a\u4e24\u4e2a\u8868\u8fdb\u884c\u8fde\u63a5\uff0c\u4ece\u800c\u6bd4\u8f83\u4e0d\u540c\u884c\u7684\u6570\u636e\u3002\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5f53\u6211\u4eec\u9700\u8981\u5728\u540c\u4e00\u5f20\u8868\u4e2d\u67e5\u627e\u76f8\u5173\u6570\u636e\u65f6\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5bf9MySQL\u7684SELF JOIN\u64cd\u4f5c\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u4e86\u89e3\u66f4\u591a\u5173\u4e8e<a href=\"https:\/\/www.mysql.com\/\" rel=\"nofollow noopener\" target=\"_blank\">MySQL<\/a>\u7684\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<h3 id=\"%e9%a6%99%e6%b8%af%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%a6%96%e9%80%89%e5%90%8e%e6%b5%aa%e4%ba%91-IlgMFuhHxl\">\u9999\u6e2f\u670d\u52a1\u5668\u9996\u9009\u540e\u6d6a\u4e91<\/h3>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5bfb\u627e\u53ef\u9760\u7684\u9999\u6e2f\u670d\u52a1\u5668\u63d0\u4f9b\u5546\uff0c\u6211\u4eec\u63a8\u8350\u60a8\u9009\u62e9\u540e\u6d6a\u4e91\u3002\u540e\u6d6a\u4e91\u63d0\u4f9b\u9ad8\u6027\u80fd\u7684\u9999\u6e2f\u670d\u52a1\u5668\uff0c\u4ee5\u53ca\u5176\u4ed6\u591a\u79cd\u4e91\u8ba1\u7b97\u4ea7\u54c1\u3002\u60a8\u53ef\u4ee5\u5728<a href=\"https:\/\/idc.net\">https:\/\/idc.net<\/a>\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528SELF JOIN\u5c06\u8868\u81ea\u8eab\u4f5c\u4e3a\u4e24\u4e2a\u8868\u8fdb\u884c\u81ea\u8fde\u63a5 MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528SELF JO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[197851],"tags":[],"class_list":["post-217803","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=217803"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217803\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=217803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=217803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=217803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}