{"id":7717,"date":"2024-11-14T09:41:26","date_gmt":"2024-11-14T01:41:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7717\/"},"modified":"2024-11-14T09:41:26","modified_gmt":"2024-11-14T01:41:26","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:\/\/fwq.ai\/blog\/7717\/","title":{"rendered":"\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206\u7ea7\u6388\u6743\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173119704673608.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206\u7ea7\u6388\u6743\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206\u7ea7\u6388\u6743\u7ed3\u6784\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u9ad8\u6548\u83b7\u53d6  \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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728MySQL\u4e2d\u9ad8\u6548\u83b7\u53d6\u7528\u6237\u5206\u7ea7\u6388\u6743\u7ed3\u6784\uff1f\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>\u5982\u4f55\u9ad8\u6548\u83b7\u53d6 \u4e2d\u7528\u6237\u88ab\u6388\u6743\u7684\u5206\u7ea7\u7ed3\u6784 \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 \u5206\u7ea7\u7ed3\u6784 \u672c\u6587\u4ee5\u8fd9\u6837\u7684\u5206\u7ea7\u7ed3\u6784\u4e3a\u4f8b\uff1a d &gt; e &gt; f \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d d \u8868\u793a\u90e8\u95e8\uff0ce \u8868\u793a\u533a\u57df\uff0cf \u8868\u793a\u6743\u9650\u3002 \u8868\u7ed3\u6784 \u8bbe\u8ba1\u5982\u4e0b\u7684\u8868\u7ed3\u6784\uff1a user [ u_id, &#8230; ] d [ d_id, d_name &#8230; ] e [ e_id, d_id, e_name &#8230; ] f [ f_id, d_id, e_id, f_name, f_area &#8230; ] p [ p_id, u_id, d_id, e_id, f_id, a_code [&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-7717","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7717","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=7717"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7717\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}