
{"id":873,"date":"2021-09-22T18:28:16","date_gmt":"2021-09-22T10:28:16","guid":{"rendered":"http:\/\/dba.qishuo.xin\/?p=873"},"modified":"2021-10-28T15:44:44","modified_gmt":"2021-10-28T07:44:44","slug":"mariadb-%e9%80%92%e5%bd%92sql%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"http:\/\/dba.qishuo.xin\/?p=873","title":{"rendered":"MariaDB \u9012\u5f52SQL\u793a\u4f8b"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<label for=\"ez-toc-cssicon-toggle-item-69f4981e87b02\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f4981e87b02\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/dba.qishuo.xin\/?p=873\/#1_%E4%BD%BF%E7%94%A8_RECURSIVE_%E5%85%B3%E9%94%AE%E5%AD%97%E5%AE%9E%E7%8E%B0%E9%80%92%E5%BD%92\" >1. \u4f7f\u7528 RECURSIVE \u5173\u952e\u5b57\u5b9e\u73b0\u9012\u5f52<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/dba.qishuo.xin\/?p=873\/#11_%E4%BB%8E%E7%88%B6%E5%88%B0%E5%AD%90%E9%80%92%E5%BD%92\" >1.1. \u4ece\u7236\u5230\u5b50\u9012\u5f52<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/dba.qishuo.xin\/?p=873\/#12_%E4%BB%8E%E5%AD%90%E5%88%B0%E7%88%B6%E9%80%92%E5%BD%92\" >1.2. \u4ece\u5b50\u5230\u7236\u9012\u5f52<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/dba.qishuo.xin\/?p=873\/#2_%E4%BD%BF%E7%94%A8%E5%8F%98%E9%87%8F%E5%AE%9E%E7%8E%B0%E9%80%92%E5%BD%92\" >2. \u4f7f\u7528\u53d8\u91cf\u5b9e\u73b0\u9012\u5f52<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/dba.qishuo.xin\/?p=873\/#21_%E4%BB%8E%E5%AD%90%E5%88%B0%E7%88%B6%E9%80%92%E5%BD%92\" >2.1. \u4ece\u5b50\u5230\u7236\u9012\u5f52<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"1_%E4%BD%BF%E7%94%A8_RECURSIVE_%E5%85%B3%E9%94%AE%E5%AD%97%E5%AE%9E%E7%8E%B0%E9%80%92%E5%BD%92\"><\/span>1. \u4f7f\u7528 RECURSIVE \u5173\u952e\u5b57\u5b9e\u73b0\u9012\u5f52<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"11_%E4%BB%8E%E7%88%B6%E5%88%B0%E5%AD%90%E9%80%92%E5%BD%92\"><\/span>1.1. \u4ece\u7236\u5230\u5b50\u9012\u5f52<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-sql line-numbers\">WITH RECURSIVE depinfo(accdepid,parentid,accdepname,lev,idpath,namepath)\n        AS (SELECT a.accdepid,\n                   a.parentid,\n                   a.accdepname,\n                   1 AS lev,\n                   CONCAT('\/', Cast(a.accdepid AS VARCHAR(4000)),'\/') AS idpath,\n                   CONCAT('\/', a.accdepname,'\/') AS namepath\n              FROM sc_accdep_bak a\n             WHERE a.parentid = -1\n               AND a.epid = 1\n               -- AND a.remark = '\u6d4b\u8bd5\u9012\u5f52'\n             UNION ALL\n            SELECT b.accdepid,\n                   b.parentid,\n                   b.accdepname,\n                   x.lev + 1,\n                   CONCAT(x.idpath,Cast(b.accdepid AS VARCHAR(4000)),'\/') as idpath,\n                   CONCAT(x.namepath,b.accdepname,'\/') AS namepath\n              FROM depinfo x\n             INNER JOIN (SELECT *\n                           FROM sc_accdep_bak y\n                          WHERE y.epid = 1\n                            AND y.parentid &lt;&gt; -1\n                            -- AND y.remark = '\u6d4b\u8bd5\u9012\u5f52'\n                        ) b ON x.accdepid = b.parentid)\nSELECT accdepid,parentid,accdepname,Lev,idpath,namepath\n  FROM depinfo;\n<\/code><\/pre>\n<p><a href=\"http:\/\/dba.qishuo.xin\/wp-content\/uploads\/2021\/10\/wp_editor_md_a126e317bbeb0131f1e5543996589c72.jpg\" data-fancybox=\"images\" data-fancybox=\"images\"><img decoding=\"async\" src=\"http:\/\/dba.qishuo.xin\/wp-content\/uploads\/2021\/10\/wp_editor_md_a126e317bbeb0131f1e5543996589c72.jpg\" alt=\"\" \/><\/a><\/p>\n<h4><span class=\"ez-toc-section\" id=\"12_%E4%BB%8E%E5%AD%90%E5%88%B0%E7%88%B6%E9%80%92%E5%BD%92\"><\/span>1.2. \u4ece\u5b50\u5230\u7236\u9012\u5f52<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-sql line-numbers\">WITH RECURSIVE depinfo(accdepid,parentid,accdepname,lev,idpath,namepath)\n        AS (SELECT a.accdepid,\n                   a.parentid,\n                   a.accdepname,\n                   1 AS lev,\n                   CONCAT('\/', Cast(a.accdepid AS VARCHAR(4000)),'\/') AS idpath,\n                   CONCAT('\/', a.accdepname,'\/') AS namepath\n              FROM sc_accdep_bak a\n             WHERE a.accdepid = 577\n               AND a.epid = 1\n             UNION ALL\n            SELECT b.accdepid,\n                   b.parentid,\n                   b.accdepname,\n                   x.lev + 1,\n                   CONCAT(x.idpath,Cast(b.accdepid AS VARCHAR(4000)),'\/') as idpath,\n                   CONCAT(x.namepath,b.accdepname,'\/') AS namepath\n              FROM depinfo x\n             INNER JOIN (SELECT *\n                           FROM sc_accdep_bak y\n                          WHERE y.epid = 1\n                        ) b ON x.parentid = b.accdepid)\nSELECT accdepid,parentid,accdepname,Lev,idpath,namepath\n  FROM depinfo;\n<\/code><\/pre>\n<p><a href=\"http:\/\/dba.qishuo.xin\/wp-content\/uploads\/2021\/10\/wp_editor_md_0332ea15c3eef1e79e92c7b0fc2609d5.jpg\" data-fancybox=\"images\" data-fancybox=\"images\"><img decoding=\"async\" src=\"http:\/\/dba.qishuo.xin\/wp-content\/uploads\/2021\/10\/wp_editor_md_0332ea15c3eef1e79e92c7b0fc2609d5.jpg\" alt=\"\" \/><\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_%E4%BD%BF%E7%94%A8%E5%8F%98%E9%87%8F%E5%AE%9E%E7%8E%B0%E9%80%92%E5%BD%92\"><\/span>2. \u4f7f\u7528\u53d8\u91cf\u5b9e\u73b0\u9012\u5f52<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"21_%E4%BB%8E%E5%AD%90%E5%88%B0%E7%88%B6%E9%80%92%E5%BD%92\"><\/span>2.1. \u4ece\u5b50\u5230\u7236\u9012\u5f52<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-sql line-numbers\">SELECT t2.module_code,\n       t2.module_name,\n       t2.parent_code,\n       t1.lev\n  FROM (SELECT @parentCode AS _parentCode,\n               (SELECT @parentCode := parent_code FROM ph_module WHERE module_code = _parentCode ) AS supdepCode,\n               @level := @level + 1 AS lev\n          FROM (SELECT @parentCode := '106010203', @level := 0) vars,\n               ph_module) t1\n INNER JOIN ph_module t2 ON t2.module_code = t1._parentCode\n ORDER BY t1.lev DESC;\n<\/code><\/pre>\n<p><a href=\"http:\/\/dba.qishuo.xin\/wp-content\/uploads\/2021\/10\/wp_editor_md_4114637890851c8090c0c920f8f71b84.jpg\" data-fancybox=\"images\" data-fancybox=\"images\"><img decoding=\"async\" src=\"http:\/\/dba.qishuo.xin\/wp-content\/uploads\/2021\/10\/wp_editor_md_4114637890851c8090c0c920f8f71b84.jpg\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4f7f\u7528 RECURSIVE \u5173\u952e\u5b57\u5b9e\u73b0\u9012\u5f52 1&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,3],"tags":[12,122],"class_list":["post-873","post","type-post","status-publish","format-standard","hentry","category-mariadb-sql","category-mariadbmysql","tag-mariadb","tag-sql"],"_links":{"self":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts\/873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=873"}],"version-history":[{"count":7,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts\/873\/revisions"}],"predecessor-version":[{"id":1044,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=\/wp\/v2\/posts\/873\/revisions\/1044"}],"wp:attachment":[{"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=873"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dba.qishuo.xin\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}