{"id":5668,"date":"2024-11-14T08:02:31","date_gmt":"2024-11-14T00:02:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5668\/"},"modified":"2024-11-14T08:02:31","modified_gmt":"2024-11-14T00:02:31","slug":"sql%e6%80%8e%e4%b9%88%e6%8b%86%e5%88%86%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5668\/","title":{"rendered":"sql\u600e\u4e48\u62c6\u5206\u5b57\u7b26\u4e32"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u62c6\u5206\u5b57\u7b26\u4e32\u6709\u51e0\u79cd\u65b9\u6cd5\uff1asplit_string() \u51fd\u6570\uff1a\u6309\u6307\u5b9a\u5206\u9694\u7b26\u62c6\u5206\uff1bsubstr() \u548c instr() \u51fd\u6570\uff1a\u6309\u7279\u5b9a\u957f\u5ea6\u62c6\u5206\uff1b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u62c6\u5206\uff1bxml\uff1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a xml \u6587\u6863\u5e76\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002\u6700\u4f73\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u96c6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053020182212955.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u62c6\u5206\u5b57\u7b26\u4e32\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u62c6\u5206\u5b57\u7b26\u4e32\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u5982\u4f55\u62c6\u5206\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u62c6\u5206\u5b57\u7b26\u4e32\u5728\u6570\u636e\u5904\u7406\u4e2d\u5341\u5206\u5e38\u89c1\uff0cSQL \u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 SPLIT_STRING() \u51fd\u6570<\/strong><\/p>\n<p>SPLIT_STRING() \u51fd\u6570\u53ef\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u4e3a\u6570\u7ec4\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SPLIT_STRING(string, delimiter)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>SELECT SPLIT_STRING('apple,banana,orange', ',')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528 SUBSTR() \u548c INSTR() \u51fd\u6570<\/strong><\/p>\n<p>SUBSTR() \u51fd\u6570\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u7684\u5b50\u5b57\u7b26\u4e32\u3002INSTR() \u51fd\u6570\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u901a\u8fc7\u5faa\u73af\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6309\u7279\u5b9a\u957f\u5ea6\u62c6\u5206\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u6bcf 3 \u4e2a\u5b57\u7b26\u62c6\u5206\uff1a<\/p>\n<pre>SET @length = 3;\nWHILE LENGTH(string) &gt; 0 DO\n    SET @substr = SUBSTR(string, 1, @length);\n    DELETE FROM string WHERE LENGTH(string) &gt;= @length;\n    -- \u5904\u7406 @substr\nEND WHILE;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>SQL \u652f\u6301\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u67e5\u627e\u548c\u5206\u5272\u5b57\u7b26\u4e32\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>REGEXP_EXTRACT(string, pattern)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u6570\u5b57\u62c6\u5206\uff1a<\/p>\n<pre>SELECT REGEXP_EXTRACT('123-456-789', '[0-9]+')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4f7f\u7528 XML<\/strong><\/p>\n<p>XML \u63d0\u4f9b\u4e86\u4e00\u79cd\u5c42\u6b21\u5316\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u8868\u793a\u5b57\u7b26\u4e32\u4e2d\u7684\u5d4c\u5957\u5143\u7d20\u3002\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a XML \u6587\u6863\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u63d0\u53d6\u548c\u5904\u7406\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u6309 XML \u6807\u7b7e\u62c6\u5206\uff1a<\/p>\n<pre>SELECT EXTRACTVALUE(xml, '\/root\/item') FROM (\n    SELECT CAST('&lt;root&gt;&lt;item&gt;' || string || '&lt;\/item&gt;&lt;\/root&gt;' AS XML) AS xml\n) AS t<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u54ea\u79cd\u65b9\u6cd5\u6700\u4f73\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u96c6\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u62c6\u5206\uff0cSPLIT_STRING() \u51fd\u6570\u901a\u5e38\u662f\u6700\u6709\u6548\u548c\u6700\u7b80\u5355\u7684\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u62c6\u5206\uff0c\u5219\u9700\u8981\u8003\u8651\u4f7f\u7528 SUBSTR() \u548c INSTR() \u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u6216 XML\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u62c6\u5206\u5b57\u7b26\u4e32\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>sql \u4e2d\u62c6\u5206\u5b57\u7b26\u4e32\u6709\u51e0\u79cd\u65b9\u6cd5\uff1asplit_string() \u51fd\u6570\uff1a\u6309\u6307\u5b9a\u5206\u9694\u7b26\u62c6\u5206\uff1bsubstr() \u548c instr() \u51fd\u6570\uff1a\u6309\u7279\u5b9a\u957f\u5ea6\u62c6\u5206\uff1b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u62c6\u5206\uff1bxml\uff1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a xml \u6587\u6863\u5e76\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002\u6700\u4f73\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u96c6\u3002 SQL \u4e2d\u5982\u4f55\u62c6\u5206\u5b57\u7b26\u4e32 \u62c6\u5206\u5b57\u7b26\u4e32\u5728\u6570\u636e\u5904\u7406\u4e2d\u5341\u5206\u5e38\u89c1\uff0cSQL \u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002 1. \u4f7f\u7528 SPLIT_STRING() \u51fd\u6570 SPLIT_STRING() \u51fd\u6570\u53ef\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u4e3a\u6570\u7ec4\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SPLIT_STRING(string, delimiter) \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u5c06\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u6570\u7ec4\uff1a SELECT SPLIT_STRING(&#8216;apple,banana,orange&#8217;, &#8216;,&#8217;) \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 SUBSTR() \u548c INSTR() \u51fd\u6570 SUBSTR() \u51fd\u6570\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u7684\u5b50\u5b57\u7b26\u4e32\u3002INSTR() \u51fd\u6570\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u901a\u8fc7\u5faa\u73af\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6309\u7279\u5b9a\u957f\u5ea6\u62c6\u5206\u3002 \u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u6bcf 3 \u4e2a\u5b57\u7b26\u62c6\u5206\uff1a SET @length = 3; WHILE LENGTH(string) &gt; 0 DO SET @substr = SUBSTR(string, 1, @length); DELETE FROM [&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-5668","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5668","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=5668"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5668\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}