{"id":5636,"date":"2024-11-14T11:20:12","date_gmt":"2024-11-14T03:20:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5636\/"},"modified":"2024-11-14T11:20:12","modified_gmt":"2024-11-14T03:20:12","slug":"sql%e6%80%8e%e4%b9%88%e6%9b%bf%e6%8d%a2%e5%ad%97%e6%ae%b5%e9%87%8c%e9%9d%a2%e6%9f%90%e4%b8%80%e4%b8%aa%e7%ac%a6%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5636\/","title":{"rendered":"sql\u600e\u4e48\u66ff\u6362\u5b57\u6bb5\u91cc\u9762\u67d0\u4e00\u4e2a\u7b26\u53f7"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u53ef\u4ee5\u4f7f\u7528 replace() \u51fd\u6570\u66ff\u6362\u5b57\u6bb5\u4e2d\u7279\u5b9a\u7b26\u53f7\uff0c\u5176\u8bed\u6cd5\u4e3a replace(string, from_string, to_string)\uff1b\u53ef\u7528\u4e8e\u6279\u91cf\u66ff\u6362\u3001\u90e8\u5206\u66ff\u6362\u548c\u6b63\u5219\u66ff\u6362\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019275378456.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u66ff\u6362\u5b57\u6bb5\u91cc\u9762\u67d0\u4e00\u4e2a\u7b26\u53f7\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u66ff\u6362\u5b57\u6bb5\u91cc\u9762\u67d0\u4e00\u4e2a\u7b26\u53f7\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u66ff\u6362\u5b57\u6bb5\u4e2d\u7279\u5b9a\u7b26\u53f7<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 REPLACE() \u51fd\u6570\u8f7b\u677e\u66ff\u6362\u5b57\u6bb5\u4e2d\u7279\u5b9a\u7b26\u53f7\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>REPLACE(string, from_string, to_string)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>string: \u9700\u8981\u8fdb\u884c\u66ff\u6362\u7684\u5b57\u6bb5\u6216\u5b57\u7b26\u4e32\u3002<\/li>\n<li>from_string: \u9700\u8981\u66ff\u6362\u7684\u7b26\u53f7\u6216\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li>to_string: \u66ff\u6362\u540e\u7684\u7b26\u53f7\u6216\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a customer_name \u7684\u5b57\u6bb5\uff0c\u5176\u4e2d\u5305\u542b\u5ba2\u6237\u59d3\u540d\u3002\u6211\u4eec\u5e0c\u671b\u5c06\u59d3\u540d\u4e2d\u6240\u6709\u9017\u53f7 (,) \u66ff\u6362\u4e3a\u8fde\u5b57\u7b26 (-)\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>UPDATE customers SET customer_name = REPLACE(customer_name, ',', '-')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u66f4\u65b0\u540e\uff0ccustomer_name \u5b57\u6bb5\u4e2d\u7684\u6240\u6709\u9017\u53f7\u90fd\u5c06\u66ff\u6362\u4e3a\u8fde\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5176\u4ed6\u7528\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u6279\u91cf\u66ff\u6362\uff1a<\/strong>REPLACE() \u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7a7a\u683c\u548c\u9017\u53f7\u66ff\u6362\u4e3a\u4e0b\u5212\u7ebf\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\uff1a<\/li>\n<\/ul>\n<pre>REPLACE(string, ' ', '_')\nREPLACE(string, ',', '_')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li> <strong>\u90e8\u5206\u66ff\u6362\uff1a<\/strong>REPLACE() \u51fd\u6570\u8fd8\u652f\u6301\u90e8\u5206\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u524d\u4e24\u4e2a\u5b57\u7b26\u66ff\u6362\u4e3a AB\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\uff1a<\/li>\n<\/ul>\n<pre>REPLACE(string, SUBSTR(string, 1, 2), 'AB')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li> <strong>\u6b63\u5219\u66ff\u6362\uff1a<\/strong>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u66ff\u6362\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e REPLACE() \u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u66ff\u6362\u4e3a *\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\uff1a<\/li>\n<\/ul>\n<pre>REPLACE(string, '[0-9]+', '*')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u66ff\u6362\u5b57\u6bb5\u91cc\u9762\u67d0\u4e00\u4e2a\u7b26\u53f7\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\u53ef\u4ee5\u4f7f\u7528 replace() \u51fd\u6570\u66ff\u6362\u5b57\u6bb5\u4e2d\u7279\u5b9a\u7b26\u53f7\uff0c\u5176\u8bed\u6cd5\u4e3a replace(string, from_string, to_string)\uff1b\u53ef\u7528\u4e8e\u6279\u91cf\u66ff\u6362\u3001\u90e8\u5206\u66ff\u6362\u548c\u6b63\u5219\u66ff\u6362\u3002 SQL \u4e2d\u66ff\u6362\u5b57\u6bb5\u4e2d\u7279\u5b9a\u7b26\u53f7 \u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 REPLACE() \u51fd\u6570\u8f7b\u677e\u66ff\u6362\u5b57\u6bb5\u4e2d\u7279\u5b9a\u7b26\u53f7\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a REPLACE(string, from_string, to_string) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a string: \u9700\u8981\u8fdb\u884c\u66ff\u6362\u7684\u5b57\u6bb5\u6216\u5b57\u7b26\u4e32\u3002 from_string: \u9700\u8981\u66ff\u6362\u7684\u7b26\u53f7\u6216\u5b50\u5b57\u7b26\u4e32\u3002 to_string: \u66ff\u6362\u540e\u7684\u7b26\u53f7\u6216\u5b50\u5b57\u7b26\u4e32\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a customer_name \u7684\u5b57\u6bb5\uff0c\u5176\u4e2d\u5305\u542b\u5ba2\u6237\u59d3\u540d\u3002\u6211\u4eec\u5e0c\u671b\u5c06\u59d3\u540d\u4e2d\u6240\u6709\u9017\u53f7 (,) \u66ff\u6362\u4e3a\u8fde\u5b57\u7b26 (-)\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a UPDATE customers SET customer_name = REPLACE(customer_name, &#8216;,&#8217;, &#8216;-&#8216;) \u767b\u5f55\u540e\u590d\u5236 \u66f4\u65b0\u540e\uff0ccustomer_name \u5b57\u6bb5\u4e2d\u7684\u6240\u6709\u9017\u53f7\u90fd\u5c06\u66ff\u6362\u4e3a\u8fde\u5b57\u7b26\u3002 \u5176\u4ed6\u7528\u6cd5\uff1a \u6279\u91cf\u66ff\u6362\uff1aREPLACE() \u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7a7a\u683c\u548c\u9017\u53f7\u66ff\u6362\u4e3a\u4e0b\u5212\u7ebf\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\uff1a REPLACE(string, &#8216; &#8216;, &#8216;_&#8217;) REPLACE(string, &#8216;,&#8217;, &#8216;_&#8217;) \u767b\u5f55\u540e\u590d\u5236 \u90e8\u5206\u66ff\u6362\uff1aREPLACE() [&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-5636","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5636","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=5636"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5636\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}