{"id":26734,"date":"2024-11-24T17:57:48","date_gmt":"2024-11-24T09:57:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26734\/"},"modified":"2024-11-24T17:57:48","modified_gmt":"2024-11-24T09:57:48","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e-mysql-%e9%80%97%e5%8f%b7%e5%88%86%e9%9a%94%e7%9a%84%e5%ad%97%e6%ae%b5%e4%b8%ad%e6%8f%90%e5%8f%96%e5%8d%95%e7%8b%ac%e5%80%bc%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26734\/","title":{"rendered":"\u5982\u4f55\u4ece MySQL \u9017\u53f7\u5206\u9694\u7684\u5b57\u6bb5\u4e2d\u63d0\u53d6\u5355\u72ec\u503c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173105406469102.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4ece MySQL \u9017\u53f7\u5206\u9694\u7684\u5b57\u6bb5\u4e2d\u63d0\u53d6\u5355\u72ec\u503c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4ece MySQL \u9017\u53f7\u5206\u9694\u7684\u5b57\u6bb5\u4e2d\u63d0\u53d6\u5355\u72ec\u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4ece  \u4e2d\u9017\u53f7\u5206\u9694\u7684\u5b57\u6bb5\u4e2d\u63d0\u53d6\u5355\u72ec\u503c<\/strong><\/p>\n<p>\u60a8\u6709\u4e00\u5f20\u5305\u542b\u5b57\u6bb5\u503c\u4ee5\u9017\u53f7\u5206\u9694\u7684\u8868\u3002\u4f8b\u5982\uff0c\u8be5\u5b57\u6bb5\u53ef\u80fd\u5305\u542b\u503c\u4e3a\u201c1,2,3,4,5,6\u201d\u8fd9\u6837\u7684\u6570\u636e\u3002\u60a8\u5e0c\u671b\u5c06\u6bcf\u4e2a\u503c\u63d0\u53d6\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 substring_index() \u51fd\u6570<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u6b64\u76ee\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 mysql \u5185\u7f6e\u51fd\u6570 substring_index()\u3002\u6b64\u51fd\u6570\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>substring_index(str,delim,count)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>str<\/strong> \u662f\u8981\u5904\u7406\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li> <strong>delim<\/strong> \u662f\u5206\u9694\u7b26\u3002<\/li>\n<li> <strong>count<\/strong> \u6307\u5b9a\u8981\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32\u7684\u5e8f\u53f7\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u5c06\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u5e76\u4e3a\u8ba1\u6570\u6307\u5b9a\u9002\u5f53\u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u9010\u4e2a\u63d0\u53d6\u9017\u53f7\u5206\u9694\u7684\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u63d0\u53d6\u201c1,2,3,4,5,6\u201d\u4e2d\u7684\u524d\u4e09\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/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)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre>+------+\n| val  |\n+------+\n| 1    |\n| 2    |\n| 3    |\n+------+<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a union all \u8bed\u53e5\u6765\u63d0\u53d6\u66f4\u591a\u503c\u3002<\/p>\n<p><strong>\u5b9e\u9645\u793a\u4f8b<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4ece MySQL \u9017\u53f7\u5206\u9694\u7684\u5b57\u6bb5\u4e2d\u63d0\u53d6\u5355\u72ec\u503c\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4ece \u4e2d\u9017\u53f7\u5206\u9694\u7684\u5b57\u6bb5\u4e2d\u63d0\u53d6\u5355\u72ec\u503c \u60a8\u6709\u4e00\u5f20\u5305\u542b\u5b57\u6bb5\u503c\u4ee5\u9017\u53f7\u5206\u9694\u7684\u8868\u3002\u4f8b\u5982\uff0c\u8be5\u5b57\u6bb5\u53ef\u80fd\u5305\u542b\u503c\u4e3a\u201c1,2,3,4,5,6\u201d\u8fd9\u6837\u7684\u6570\u636e\u3002\u60a8\u5e0c\u671b\u5c06\u6bcf\u4e2a\u503c\u63d0\u53d6\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u8bb0\u5f55\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 substring_index() \u51fd\u6570 \u8981\u5b9e\u73b0\u6b64\u76ee\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 mysql \u5185\u7f6e\u51fd\u6570 substring_index()\u3002\u6b64\u51fd\u6570\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a substring_index(str,delim,count) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a str \u662f\u8981\u5904\u7406\u7684\u5b57\u7b26\u4e32\u3002 delim \u662f\u5206\u9694\u7b26\u3002 count \u6307\u5b9a\u8981\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32\u7684\u5e8f\u53f7\u3002 \u901a\u8fc7\u5c06\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u5e76\u4e3a\u8ba1\u6570\u6307\u5b9a\u9002\u5f53\u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u9010\u4e2a\u63d0\u53d6\u9017\u53f7\u5206\u9694\u7684\u503c\u3002 \u4f8b\u5982\uff0c\u8981\u63d0\u53d6\u201c1,2,3,4,5,6\u201d\u4e2d\u7684\u524d\u4e09\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a select substring_index(&#8216;1,2,3,4,5,6&#8217;, &#8216;,&#8217;, 1) as val union all select substring_index(substring_index(&#8216;1,2,3,4,5,6&#8217;, &#8216;,&#8217;, 2), &#8216;,&#8217;, -1) union all select substring_index(substring_index(&#8216;1,2,3,4,5,6&#8217;, &#8216;,&#8217;, 3), &#8216;,&#8217;, -1) \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u8fd4\u56de\u4ee5\u4e0b\u8f93\u51fa\uff1a +&#8212;&#8212;+ | val | +&#8212;&#8212;+ | 1 | | 2 [&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-26734","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26734","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=26734"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26734\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}