{"id":200829,"date":"2025-05-06T12:11:49","date_gmt":"2025-05-06T04:11:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200829\/"},"modified":"2025-05-06T12:11:49","modified_gmt":"2025-05-06T04:11:49","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e-mysql-%e5%ad%97%e6%ae%b5%e4%b8%ad%e5%8c%85%e5%90%ab%e9%80%97%e5%8f%b7%e5%88%86%e9%9a%94%e7%9a%84%e5%80%bc%e4%b8%ad%e6%8f%90%e5%8f%96%e5%8d%95%e4%b8%aa%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200829\/","title":{"rendered":"\u5982\u4f55\u4ece MySQL \u5b57\u6bb5\u4e2d\u5305\u542b\u9017\u53f7\u5206\u9694\u7684\u503c\u4e2d\u63d0\u53d6\u5355\u4e2a\u503c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4ece MySQL \u5b57\u6bb5\u4e2d\u5305\u542b\u9017\u53f7\u5206\u9694\u7684\u503c\u4e2d\u63d0\u53d6\u5355\u4e2a\u503c\uff1f<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/173173306467382648a4d30.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4ece\u5305\u542b\u9017\u53f7\u5206\u9694\u503c\u7684 mysql \u5b57\u6bb5\u4e2d\u63d0\u53d6\u5355\u4e2a\u503c<\/strong><\/p>\n<p>\u60a8\u8981\u4ece\u4e00\u4e2a\u503c\u4e2d\u63d0\u53d6\u591a\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u503c\uff0c\u4f8b\u5982\u201c1,2,3,4,5,6\u201d\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 mysql \u51fd\u6570 substring_index() \u6765\u5b9e\u73b0\u6b64\u76ee\u6807\uff1a<\/p>\n<p><strong>\u5177\u4f53\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528 substring_index() \u51fd\u6570\u6309\u9017\u53f7\u5206\u9694\u503c\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u591a\u884c\u3002<\/li>\n<li>\u91cd\u590d\u4f7f\u7528 substring_index() \u6765\u8fdb\u4e00\u6b65\u5206\u9694\u540e\u7684\u884c\uff0c\u76f4\u5230\u63d0\u53d6\u5230\u6240\u9700\u7684\u503c\u3002<\/li>\n<\/ol>\n<p><strong>sql \u8bed\u53e5\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>select substring_index('1,2,3,4,5,6', ',', 1) as val\nunion all select substring_index(substring_index('1,2,3,4,5,6', ',', 2), ',', -1)\nunion all select substring_index(substring_index('1,2,3,4,5,6', ',', 3), ',', -1)\n-- \u589e\u52a0\u66f4\u591a union all select \u8bed\u53e5\u4ee5\u63d0\u53d6\u5176\u4ed6\u503c<\/pre>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u4e00\u4e2a\u8868\uff0c\u5176\u4e2d\u5305\u542b\u5355\u72ec\u63d0\u53d6\u7684\u503c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>val<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u6d4b\u8bd5\uff1a<\/strong><\/p>\n<pre>mysql&gt; SELECT SUBSTRING_INDEX('1,2,3,4,5,6', ',', 1) AS val\n    -&gt; UNION ALL SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('1,2,3,4,5,6', ',', 2), ',', -1)\n    -&gt; UNION ALL SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('1,2,3,4,5,6', ',', 3), ',', -1)\n    -&gt; ;\n+------+\n| val  |\n+------+\n| 1    |\n| 2    |\n| 3    |\n+------+\n3 rows in set (0.00 sec)\n\nmysql&gt; <\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4ece MySQL \u5b57\u6bb5\u4e2d\u5305\u542b\u9017\u53f7\u5206\u9694\u7684\u503c\u4e2d\u63d0\u53d6\u5355\u4e2a\u503c\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6570\u636e\u5e93\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4ece MySQL \u5b57\u6bb5\u4e2d\u5305\u542b\u9017&#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-200829","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200829","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=200829"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200829\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}