{"id":201098,"date":"2025-05-06T10:23:09","date_gmt":"2025-05-06T02:23:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201098\/"},"modified":"2025-05-06T10:23:09","modified_gmt":"2025-05-06T02:23:09","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8mysql%e4%b8%ad%e9%ab%98%e6%95%88%e8%8e%b7%e5%8f%96%e7%94%a8%e6%88%b7%e5%88%86%e7%ba%a7%e6%8e%88%e6%9d%83%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201098\/","title":{"rendered":"\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206\u7ea7\u6388\u6743\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206\u7ea7\u6388\u6743\u7ed3\u6784\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u4e3b\u673a\u5b9d\u8d1d\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733020772674bcc644da0a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u9ad8\u6548\u83b7\u53d6 mysql \u4e2d\u7528\u6237\u88ab\u6388\u6743\u7684\u5206\u7ea7\u7ed3\u6784<\/strong><\/p>\n<p>\u5f88\u591a\u65f6\u5019\uff0c\u9700\u8981\u6839\u636e\u7528\u6237\u7684\u6743\u9650\u5bf9\u6570\u636e\u8fdb\u884c\u7b5b\u9009\uff0c\u6743\u9650\u662f\u5206\u7ea7\u7684\uff0c\u6bd4\u5982\u90e8\u95e8\u3001\u533a\u57df\u3001\u89d2\u8272\u7b49\u3002\u5728 mysql \u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u83b7\u53d6\u7528\u6237\u88ab\u6388\u6743\u7684\u5206\u7ea7\u7ed3\u6784\u5462\uff1f<\/p>\n<p><strong>\u5206\u7ea7\u7ed3\u6784<\/strong><\/p>\n<p>\u672c\u6587\u4ee5\u8fd9\u6837\u7684\u5206\u7ea7\u7ed3\u6784\u4e3a\u4f8b\uff1a<\/p>\n<pre>d &gt; e &gt; f<\/pre>\n<p>\u5176\u4e2d d \u8868\u793a\u90e8\u95e8\uff0ce \u8868\u793a\u533a\u57df\uff0cf \u8868\u793a\u6743\u9650\u3002<\/p>\n<p><strong>\u8868\u7ed3\u6784<\/strong><\/p>\n<p>\u8bbe\u8ba1\u5982\u4e0b\u7684\u8868\u7ed3\u6784\uff1a<\/p>\n<pre>user [ u_id, ... ]\nd    [ d_id, d_name ... ]\ne    [ e_id, d_id, e_name ... ]\nf    [ f_id, d_id, e_id, f_name, f_area ... ]\np    [ p_id, u_id, d_id, e_id, f_id, a_code ... ]<\/pre>\n<p>\u5176\u4e2d\uff0cp \u8868\u662f\u6388\u6743\u8868\u3002\u5f53 u_id\u3001d_id\u3001e_id\u3001f_id \u4e3a 0 \u65f6\uff0c\u89c6\u4e3a\u6388\u6743\u6a21\u5f0f\u4e2d\u7684 &#8220;-&#8220;\uff0c\u5373\u5168\u90e8\u6388\u6743\u3002\u5f53 a_code \u4e3a\u7a7a\u5b57\u7b26\u4e32\u65f6\uff0c\u89c6\u4e3a\u6388\u6743\u6a21\u5f0f\u4e2d\u7684 &#8220;-&#8220;\uff0c\u5373\u5168\u90e8\u6388\u6743\u3002<\/p>\n<p><strong>\u6388\u6743\u6a21\u5f0f<\/strong><\/p>\n<p>\u6388\u6743\u6a21\u5f0f\u5171\u6709 7 \u79cd\uff1a<\/p>\n<pre>[1] - - - - \/\/ \u6388\u6743\u6240\u6709 d , e , f\n[2] d - - - \/\/ \u6388\u6743 d\uff0c\u5305\u62ec d \u4e0b\u6240\u6709 e \u548c f\n[3] d e - - \/\/ \u6388\u6743 d &gt; e\uff0c\u5305\u62ec e \u4e0b\u6240\u6709 f\n[4] d e f - \/\/ \u6388\u6743 d &gt; e &gt; f\n[5] - - - a \/\/ \u6388\u6743 a\uff0c\u5305\u62ec \u6240\u6709\u5c5e\u4e8e a \u7684 f\n[6] d - - a \/\/ \u6388\u6743 d \u4e0b\u7684 a\uff0c\u542bd \u4e0b\u6240\u6709 \u5728 a \u7684 f\n[7] d e - a \/\/ \u6388\u6743 d &gt; e \u4e0b\u7684 a\uff0c\u542be \u4e0b\u6240\u6709 \u5728 a \u7684 f<\/pre>\n<p><strong>\u6388\u6743\u5217\u8868<\/strong><\/p>\n<p>\u9700\u8981\u83b7\u53d6\u67d0\u4e2a\u7528\u6237\u88ab\u6388\u6743\u7684 f\u3001e\u3001d \u5217\u8868\u3002\u8981\u8003\u8651\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u7528\u6237\u901a\u8fc7\u6388\u6743\u6a21\u5f0f &#8211; &#8211; &#8211; a\uff0c\u53ef\u4ee5\u83b7\u5f97\u90e8\u5206 d\u3001e\u3001f \u7684\u6388\u6743<\/li>\n<li>\u7528\u6237\u901a\u8fc7\u6388\u6743\u6a21\u5f0f d &#8211; &#8211; a\uff0c\u53ef\u4ee5\u83b7\u5f97\u90e8\u5206 e\u3001f \u7684\u6388\u6743<\/li>\n<li>\u7528\u6237\u901a\u8fc7\u6388\u6743\u6a21\u5f0f d e &#8211; a\uff0c\u53ef\u4ee5\u83b7\u5f97\u90e8\u5206 f \u7684\u6388\u6743<\/li>\n<li>\u5f53\u6709 d &gt; e\uff0c\u4e14 d &gt; e \u4e0b\u6ca1\u6709 f \u65f6\uff0c\u5982\u679c\u6709\u5bf9\u5e94\u6388\u6743\uff0c\u4e5f\u9700\u8981\u9009\u51fa d \u548c e<\/li>\n<\/ul>\n<p><strong>\u5206\u9875\u83b7\u53d6<\/strong><\/p>\n<p>f \u5217\u8868\u7684\u6570\u91cf\u7ea7\u53ef\u80fd\u5f88\u5927\uff0c\u9700\u8981\u5206\u9875\u83b7\u53d6\u3002<\/p>\n<p><strong>mysql \u67e5\u8be2<\/strong><\/p>\n<p>\u67e5\u8be2\u5176\u4e2d\u4e00\u4e2a\u7528\u6237\u88ab\u6388\u6743\u7684 f \u5217\u8868\uff1a<\/p>\n<pre>(select f.f_id, f.f_name, f.f_area\n from p \n join f on p.f_id = f.f_id or p.f_id = 0\n where p.u_id = @user_id and (p.d_id = f.d_id or p.d_id = 0) \n and (p.e_id = f.e_id or p.e_id = 0) \n and (p.a_code = f.f_area or p.a_code = \"\")\n order by f.f_id \n limit x, y) -- x \u662f\u8d77\u59cb\u7d22\u5f15\uff0cy \u662f\u6bcf\u9875\u7684\u6570\u91cf<\/pre>\n<p>\u67e5\u8be2\u5176\u4e2d\u4e00\u4e2a\u7528\u6237\u88ab\u6388\u6743\u7684 e \u5217\u8868\uff1a<\/p>\n<pre>(select e.e_id, e.e_name\n from p\n join e on p.e_id = e.e_id or p.e_id = 0\n where p.u_id = @user_id and (p.d_id = e.d_id or p.d_id = 0)\n order by e.e_id\n limit x, y)<\/pre>\n<p>\u67e5\u8be2\u5176\u4e2d\u4e00\u4e2a\u7528\u6237\u88ab\u6388\u6743\u7684 d \u5217\u8868\uff1a<\/p>\n<pre>(SELECT D.D_id, D.D_name\n FROM P\n JOIN D ON P.D_id = D.D_id OR P.D_id = 0\n WHERE P.U_id = @user_id\n ORDER BY D.D_id\n LIMIT x, y)<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206\u7ea7\u6388\u6743\u7ed3\u6784\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201098","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201098"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201098\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}