{"id":5083,"date":"2020-08-23T19:57:42","date_gmt":"2020-08-23T11:57:42","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5083"},"modified":"2020-08-23T19:57:42","modified_gmt":"2020-08-23T11:57:42","slug":"mysql-%e9%80%92%e5%bd%92%e6%9f%a5%e8%af%a2%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5083","title":{"rendered":"MySQL \u9012\u5f52\u67e5\u8be2\u6559\u7a0b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u6709\u7f51\u53cb\u95ee\u9898\u6211 MySQL \u5982\u4f55\u5b9e\u73b0\u9012\u5f52\u67e5\u8be2\uff1f\u6211\u968f\u624b\u53d1\u4ed6\u4e86\u4e00\u4e2a<a rel=\"noreferrer noopener\" aria-label=\"MySQL\u89c6\u9891\u6559\u7a0b\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"https:\/\/www.bilibili.com\/video\/BV1Yh411d74y\" target=\"_blank\">MySQL\u89c6\u9891\u6559\u7a0b<\/a>\uff0c\u8c01\u77e5\u4ed6\u5f00\u59cb\u5410\u69fd\u6211\u4e86\u3002\u201c\u4e0d\u4f1a\u5c31\u4e0d\u4f1a\uff0c\u6559\u7a0b\u6211\u81ea\u5df1\u4e0d\u4f1a\u641c\uff1f\u8fd8\u9700\u8981\u4f60\u7ed9\u6211\u641c\uff1f\u592a\u6253\u51fb\u4eba\u4e86\u201d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u771f\u662f\u592a\u96be\u4e86\uff0c\u53d1\u7ed9\u4ed6\u4e00\u4e2a\u6559\u7a0b\u8fd8\u6709\u9519\u4e86\u4e0d\u6210\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u5f52\u8bf4\uff0c\u4eca\u5929\u6211\u4eec\u8fd8\u662f\u4e00\u8d77\u6765\u5b9e\u73b0\u4e00\u4e2a MySQL \u9012\u5f52\u67e5\u8be2\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f17\u6240\u5468\u77e5\uff0c\u5728 Oracle \u4e2d\uff0c\u6709\u4e00\u4e2a start with connect by prior \u8bed\u6cd5\u3002\u6240\u4ee5\uff0c\u9012\u5f52\u67e5\u8be2\u5c31\u53d8\u5f97\u975e\u5e38\u7684\u7b80\u5355\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from xttblog_dept start with id='1001' connet by prior id=pid;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c MySQL \u4e2d\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u6837\u7684\u8bed\u6cd5\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u9012\u5f52\u67e5\u8be2\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u4eec\u4ee5\u90e8\u95e8\u8868\u4e3a\u4f8b\uff0c\u6765\u8bf4\u660e\u6211\u4eec\u5982\u4f55\u81ea\u5b9a\u4e49 MySQL \u9012\u5f52\u67e5\u8be2\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP TABLE IF EXISTS `xttblog_dept`;\nCREATE TABLE `xttbblog_dept`  (\n  `id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,\n  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,\n  `pid` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,\n  PRIMARY KEY (`id`) USING BTREE\n) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;\n\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1000', '\u4e1a\u4f59\u8349\u603b\u516c\u53f8', NULL);\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1001', '\u4e1a\u4f59\u8349\u5317\u4eac\u5206\u516c\u53f8', '1000');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1002', '\u4e1a\u4f59\u8349\u4e0a\u6d77\u5206\u516c\u53f8', '1000');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1003', '\u4e1a\u4f59\u8349\u5317\u4eac\u7814\u53d1\u90e8', '1001');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1004', '\u4e1a\u4f59\u8349\u5317\u4eac\u8d22\u52a1\u90e8', '1001');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1005', '\u4e1a\u4f59\u8349\u5317\u4eac\u5e02\u573a\u90e8', '1001');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1006', '\u4e1a\u4f59\u8349\u4e1a\u4f59\u8349\u5317\u4eac\u7814\u53d1\u4e00\u90e8', '1003');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1007', '\u4e1a\u4f59\u8349\u5317\u4eac\u7814\u53d1\u4e8c\u90e8', '1003');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1008', '\u4e1a\u4f59\u8349\u5317\u4eac\u7814\u53d1\u4e00\u90e8\u4e00\u5c0f\u7ec4', '1006');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1009', '\u4e1a\u4f59\u8349\u5317\u4eac\u7814\u53d1\u4e00\u90e8\u4e8c\u5c0f\u7ec4', '1006');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1010', '\u4e1a\u4f59\u8349\u5317\u4eac\u7814\u53d1\u4e8c\u90e8\u4e00\u5c0f\u7ec4', '1007');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1011', '\u4e1a\u4f59\u8349\u5317\u4eac\u7814\u53d1\u4e8c\u90e8\u4e8c\u5c0f\u7ec4', '1007');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1012', '\u4e1a\u4f59\u8349\u5317\u4eac\u5e02\u573a\u4e00\u90e8', '1005');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1013', '\u4e1a\u4f59\u8349\u4e0a\u6d77\u7814\u53d1\u90e8', '1002');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1014', '\u4e1a\u4f59\u8349\u4e0a\u6d77\u7814\u53d1\u4e00\u90e8', '1013');\nINSERT INTO `dept`(`id`, `name`, `pid`) VALUES ('1015', '\u4e1a\u4f59\u8349\u4e0a\u6d77\u7814\u53d1\u4e8c\u90e8', '1013');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u90e8\u95e8\u9012\u5f52\u51fd\u6570\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delimiter $$ \ndrop function if exists get_child_list$$ \ncreate function get_child_list(in_id varchar(10)) returns varchar(1000) \nbegin \n    declare ids varchar(1000) default ''; \n    declare tempids varchar(1000); \n \n    set tempids = in_id; \n    while tempids is not null do \n        set ids = CONCAT_WS(',',ids,tempids); \n        select GROUP_CONCAT(id) into tempids from xttblog_dept where FIND_IN_SET(pid,tempids)&gt;0;  \n    end while; \n    return ids; \nend  \n$$ \ndelimiter ; <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u9762\u7684\u53d8\u91cf\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u867d\u7136\u9012\u5f52\u529f\u80fd\u5df2\u7ecf\u5b9e\u73b0\u4e86\uff0c\u4f46\u8fd8\u662f\u6709\u7f3a\u9677\u3002\u7528\u4e86\u4e0d\u5c11\u7cfb\u7edf\u51fd\u6570\u4e0d\u8bf4\uff0c\u8fd8\u6709 1000 \u4e2a\u5b57\u7b26\u957f\u5ea6\u9650\u5236\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u662f\u4e00\u4e2a\u90e8\u95e8\u9012\u5f52\u5f80\u4e0b\u67e5\u7684\u51fd\u6570\uff0c\u5982\u679c\u6211\u9700\u8981\u6839\u636e\u5b50\u90e8\u95e8\u9012\u5f52\u67e5\u7236\u90e8\u95e8\u8be5\u600e\u4e48\u529e\u5462\uff1f\u4e0d\u5f97\u5df2\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u518d\u5b9e\u73b0\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delimiter $$ \ndrop function if exists get_parent_list$$ \ncreate function get_parent_list(in_id varchar(10)) returns varchar(1000) \nbegin \n    declare ids varchar(1000); \n    declare tempid varchar(10); \n     \n    set tempid = in_id; \n    while tempid is not null do \n        set ids = CONCAT_WS(',',ids,tempid); \n        select pid into tempid from xttblog_dept where id=tempid; \n    end while; \n    return ids; \nend \n$$ \ndelimiter ; <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u4e24\u4e2a\u51fd\u6570\u5efa\u8bae\u5927\u5bb6\u6536\u85cf\uff0c\u5e73\u65f6\u9047\u5230\u7684\u53ef\u80fd\u6027\u8fd8\u662f\u6bd4\u8f83\u5927\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7528 group_concat \u51fd\u6570\u6765\u62fc\u63a5\u5b57\u7b26\u4e32\uff0c\u5b83\u662f\u6709\u957f\u5ea6\u9650\u5236\u7684\uff0c\u9ed8\u8ba4\u4e3a 1024 \u5b57\u8282\u3002\u53ef\u4ee5\u901a\u8fc7 show variables like &#8220;group_concat_max_len&#8221;; \u6765\u67e5\u770b\u3002\u5982\u679c\u4e0d\u591f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684 SQL \u8bed\u53e5\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET GLOBAL group_concat_max_len=102400; \n-- \u6216\u8005 \nSET SESSION group_concat_max_len=102400;\n-- \u6216\u8005\u4fee\u6539 MySQL \u914d\u7f6e\u6587\u4ef6 my.cnf \uff0c\u589e\u52a0 \n# group_concat_max_len = 102400 \n#\u4f60\u8981\u7684\u6700\u5927\u957f\u5ea6 \u3002<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4e0d\u5efa\u8bae\u5927\u5bb6\u4fee\u6539\u8fd9\u4e2a\u503c\u3002\u5982\u679c\u9047\u5230 group_concat \u62fc\u63a5\u957f\u5ea6\u95ee\u9898\uff0c\u5efa\u8bae\u8fd8\u662f\u4ece\u5176\u4ed6\u4e1a\u52a1\u65b9\u9762\u4f18\u5316\u4f60\u7684\u5b9e\u73b0\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5083\">MySQL \u9012\u5f52\u67e5\u8be2\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6709\u7f51\u53cb\u95ee\u9898\u6211 MySQL \u5982\u4f55\u5b9e\u73b0\u9012\u5f52\u67e5\u8be2\uff1f\u6211\u968f\u624b\u53d1\u4ed6\u4e86\u4e00\u4e2aMySQL\u89c6\u9891\u6559\u7a0b\uff0c\u8c01\u77e5\u4ed6\u5f00\u59cb\u5410\u69fd\u6211\u4e86\u3002\u201c\u4e0d\u4f1a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[2026,2021,83,580,447],"class_list":["post-5083","post","type-post","status-publish","format-standard","hentry","category-java","tag-group-by","tag-group_concat","tag-mysql","tag-mysql----dba"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5083"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5083\/revisions"}],"predecessor-version":[{"id":5084,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5083\/revisions\/5084"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}