{"id":80487,"date":"2024-10-16T14:24:08","date_gmt":"2024-10-16T06:24:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/80487\/"},"modified":"2024-10-16T14:24:08","modified_gmt":"2024-10-16T06:24:08","slug":"mysql%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%87%bd%e6%95%b8%e8%a9%b3%e7%b4%b0%e4%bb%8b%e7%b4%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/80487\/","title":{"rendered":"MySQL\u4e2d\u5b57\u7b26\u4e32\u51fd\u6578\u8a73\u7d30\u4ecb\u7d39"},"content":{"rendered":"<h1 id=\"mysql%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%87%bd%e6%95%b8%e8%a9%b3%e7%b4%b0%e4%bb%8b%e7%b4%b9-XabjrUciQc\">MySQL\u4e2d\u5b57\u7b26\u4e32\u51fd\u6578\u8a73\u7d30\u4ecb\u7d39<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u5b57\u7b26\u4e32\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u74b0\u3002MySQL\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5b57\u7b26\u4e32\u51fd\u6578\u4f86\u5e6b\u52a9\u7528\u6236\u9032\u884c\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u548c\u8655\u7406\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39MySQL\u4e2d\u7684\u4e00\u4e9b\u5e38\u7528\u5b57\u7b26\u4e32\u51fd\u6578\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u793a\u4f8b\u4ee5\u4fbf\u65bc\u7406\u89e3\u3002<\/p>\n<h2 id=\"1-%e5%ad%97%e7%ac%a6%e4%b8%b2%e9%95%b7%e5%ba%a6%e5%87%bd%e6%95%b8-XabjrUciQc\">1. \u5b57\u7b26\u4e32\u9577\u5ea6\u51fd\u6578<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c<code>LENGTH()<\/code>\u51fd\u6578\u7528\u65bc\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u5b57\u7bc0\u9577\u5ea6\uff0c\u800c<code>CHAR_LENGTH()<\/code>\u6216<code>CHARACTER_LENGTH()<\/code>\u5247\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6578\u3002\u9019\u5169\u8005\u7684\u5340\u5225\u5728\u65bc\uff0c\u5c0d\u65bc\u67d0\u4e9b\u591a\u5b57\u7bc0\u5b57\u7b26\u96c6\uff08\u5982UTF-8\uff09\uff0c\u5b57\u7bc0\u9577\u5ea6\u53ef\u80fd\u5927\u65bc\u5b57\u7b26\u6578\u3002<\/p>\n<pre><code>SELECT LENGTH('Hello, \u4e16\u754c'); -- \u8fd4\u56de 13\nSELECT CHAR_LENGTH('Hello, \u4e16\u754c'); -- \u8fd4\u56de 9\n<\/code><\/pre>\n<h2 id=\"2-%e5%ad%97%e7%ac%a6%e4%b8%b2%e9%80%a3%e6%8e%a5%e5%87%bd%e6%95%b8-XabjrUciQc\">2. \u5b57\u7b26\u4e32\u9023\u63a5\u51fd\u6578<\/h2>\n<p>MySQL\u63d0\u4f9b\u4e86<code>CONCAT()<\/code>\u51fd\u6578\u4f86\u9023\u63a5\u591a\u500b\u5b57\u7b26\u4e32\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6578\u91cf\u7684\u53c3\u6578\uff0c\u4e26\u5c07\u5b83\u5011\u9023\u63a5\u6210\u4e00\u500b\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code>SELECT CONCAT('Hello', ', ', 'World!'); -- \u8fd4\u56de 'Hello, World!'\n<\/code><\/pre>\n<h2 id=\"3-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%9f%a5%e6%89%be%e5%87%bd%e6%95%b8-XabjrUciQc\">3. \u5b57\u7b26\u4e32\u67e5\u627e\u51fd\u6578<\/h2>\n<p>\u5982\u679c\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u67d0\u500b\u5b50\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>LOCATE()<\/code>\u6216<code>INSTR()<\/code>\u51fd\u6578\u3002\u9019\u4e9b\u51fd\u6578\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728\u4e3b\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u82e5\u672a\u627e\u5230\u5247\u8fd4\u56de0\u3002<\/p>\n<pre><code>SELECT LOCATE('World', 'Hello, World!'); -- \u8fd4\u56de 8\nSELECT INSTR('Hello, World!', 'World'); -- \u8fd4\u56de 8\n<\/code><\/pre>\n<h2 id=\"4-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%9b%bf%e6%8f%9b%e5%87%bd%e6%95%b8-XabjrUciQc\">4. \u5b57\u7b26\u4e32\u66ff\u63db\u51fd\u6578<\/h2>\n<p>\u8981\u66ff\u63db\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528<code>REPLACE()<\/code>\u51fd\u6578\u3002\u9019\u500b\u51fd\u6578\u63a5\u53d7\u4e09\u500b\u53c3\u6578\uff1a\u539f\u5b57\u7b26\u4e32\u3001\u8981\u66ff\u63db\u7684\u5b50\u5b57\u7b26\u4e32\u548c\u66ff\u63db\u5f8c\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code>SELECT REPLACE('Hello, World!', 'World', 'MySQL'); -- \u8fd4\u56de 'Hello, MySQL!'\n<\/code><\/pre>\n<h2 id=\"5-%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a4%a7%e5%b0%8f%e5%af%ab%e8%bd%89%e6%8f%9b%e5%87%bd%e6%95%b8-XabjrUciQc\">5. \u5b57\u7b26\u4e32\u5927\u5c0f\u5beb\u8f49\u63db\u51fd\u6578<\/h2>\n<p>MySQL\u63d0\u4f9b\u4e86<code>UPPER()<\/code>\u548c<code>LOWER()<\/code>\u51fd\u6578\u4f86\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5927\u5beb\u6216\u5c0f\u5beb\u3002<\/p>\n<pre><code>SELECT UPPER('Hello, World!'); -- \u8fd4\u56de 'HELLO, WORLD!'\nSELECT LOWER('Hello, World!'); -- \u8fd4\u56de 'hello, world!'\n<\/code><\/pre>\n<h2 id=\"6-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%aa%e5%8f%96%e5%87%bd%e6%95%b8-XabjrUciQc\">6. \u5b57\u7b26\u4e32\u622a\u53d6\u51fd\u6578<\/h2>\n<p>\u5982\u679c\u9700\u8981\u5f9e\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>SUBSTRING()<\/code>\u51fd\u6578\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u9577\u5ea6\u3002<\/p>\n<pre><code>SELECT SUBSTRING('Hello, World!', 8, 5); -- \u8fd4\u56de 'World'\n<\/code><\/pre>\n<h2 id=\"7-%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a1%ab%e5%85%85%e5%87%bd%e6%95%b8-XabjrUciQc\">7. \u5b57\u7b26\u4e32\u586b\u5145\u51fd\u6578<\/h2>\n<p>MySQL\u9084\u63d0\u4f9b\u4e86<code>LPAD()<\/code>\u548c<code>RPAD()<\/code>\u51fd\u6578\uff0c\u7528\u65bc\u5728\u5b57\u7b26\u4e32\u7684\u5de6\u5074\u6216\u53f3\u5074\u586b\u5145\u6307\u5b9a\u7684\u5b57\u7b26\uff0c\u4ee5\u9054\u5230\u6307\u5b9a\u7684\u9577\u5ea6\u3002<\/p>\n<pre><code>SELECT LPAD('123', 5, '0'); -- \u8fd4\u56de '00123'\nSELECT RPAD('123', 5, '0'); -- \u8fd4\u56de '12300'\n<\/code><\/pre>\n<h2 id=\"8-%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2%e5%87%bd%e6%95%b8-XabjrUciQc\">8. \u5b57\u7b26\u4e32\u5206\u5272\u51fd\u6578<\/h2>\n<p>\u96d6\u7136MySQL\u6c92\u6709\u76f4\u63a5\u7684\u5b57\u7b26\u4e32\u5206\u5272\u51fd\u6578\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>SUBSTRING_INDEX()<\/code>\u4f86\u9054\u5230\u985e\u4f3c\u7684\u6548\u679c\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u6839\u64da\u6307\u5b9a\u7684\u5206\u9694\u7b26\u4f86\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u67d0\u4e00\u90e8\u5206\u3002<\/p>\n<pre><code>SELECT SUBSTRING_INDEX('apple,banana,cherry', ',', 2); -- \u8fd4\u56de 'apple,banana'\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XabjrUciQc\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u4e2d\u7684\u5b57\u7b26\u4e32\u51fd\u6578\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8655\u7406\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002\u7121\u8ad6\u662f\u67e5\u627e\u3001\u66ff\u63db\u9084\u662f\u683c\u5f0f\u5316\uff0c\u9019\u4e9b\u51fd\u6578\u90fd\u80fd\u5920\u6709\u6548\u5730\u6eff\u8db3\u9700\u6c42\u3002\u4e86\u89e3\u9019\u4e9b\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5c07\u6709\u52a9\u65bc\u63d0\u9ad8\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6548\u7387\u548c\u9748\u6d3b\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u4e86\u89e3MySQL\u4e2d\u7684\u5b57\u7b26\u4e32\u51fd\u6570\uff0c\u638c\u63e1\u5e38\u7528\u64cd\u4f5c\u4e0e\u6280\u5de7\uff0c\u63d0\u5347\u6570\u636e\u5e93\u7ba1\u7406\u4e0e\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-80487","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80487","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=80487"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80487\/revisions"}],"predecessor-version":[{"id":80488,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80487\/revisions\/80488"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=80487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=80487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=80487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}