{"id":5701,"date":"2024-11-14T10:15:04","date_gmt":"2024-11-14T02:15:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5701\/"},"modified":"2024-11-14T10:15:04","modified_gmt":"2024-11-14T02:15:04","slug":"sql%e6%80%8e%e4%b9%88%e5%8f%96%e6%9f%90%e4%b8%aa%e5%ad%97%e7%ac%a6%e5%90%8e%e7%9a%84%e5%ad%97%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5701\/","title":{"rendered":"sql\u600e\u4e48\u53d6\u67d0\u4e2a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528 substring \u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\uff1a\u8bed\u6cd5\uff1asubstring(string, start, length)string\uff1a\u5f85\u63d0\u53d6\u5b50\u4e32\u7684\u5b57\u7b26\u4e32start\uff1a\u8d77\u59cb\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\uff09length\uff1a\u5b50\u4e32\u957f\u5ea6\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053021005241059.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u53d6\u67d0\u4e2a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u53d6\u67d0\u4e2a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 SUBSTRING \u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SUBSTRING(string, start, length)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>string\uff1a\u8981\u4ece\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li>start\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\uff09\u3002<\/li>\n<li>length\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a table_name \u7684\u8868\uff0c\u5176\u4e2d\u6709\u4e00\u5217\u540d\u4e3a column_name\uff0c\u8be5\u5217\u5305\u542b\u4ee5\u4e0b\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>ABC123\nDEF456\nGHI789<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8981\u63d0\u53d6 ABC123 \u5b57\u7b26\u4e32\u4e2d\u7684 C \u5b57\u7b26\u53ca\u5176\u540e\u7684\u6240\u6709\u5b57\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT SUBSTRING(column_name, 2, LENGTH(column_name) - 1)\nFROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8be5\u67e5\u8be2\u5c06\u8fd4\u56de\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre>BC123\nEF456\nHI789<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>LENGTH(column_name) &#8211; 1 \u7528\u4e8e\u4ece\u5b50\u5b57\u7b26\u4e32\u4e2d\u6392\u9664\u8d77\u59cb\u5b57\u7b26\u3002<\/li>\n<li>\u4e5f\u53ef\u4ee5\u4f7f\u7528 LEFT \u6216 RIGHT \u51fd\u6570\u6765\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u793a\u4f8b\uff1a<\/strong><\/p>\n<ul>\n<li>\u63d0\u53d6\u5b57\u7b26\u4e32 ABCD123 \u4e2d C \u5b57\u7b26\u53ca\u5176\u540e\u7684 2 \u4e2a\u5b57\u7b26\uff1a<\/li>\n<\/ul>\n<pre>SELECT SUBSTRING(column_name, 3, 2)\nFROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>\u63d0\u53d6\u5b57\u7b26\u4e32 XYZ123 \u4e2d\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff1a<\/li>\n<\/ul>\n<pre>SELECT SUBSTRING(column_name, LENGTH(column_name), 1)\nFROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u53d6\u67d0\u4e2a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\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>\u53ef\u4ee5\u4f7f\u7528 substring \u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\uff1a\u8bed\u6cd5\uff1asubstring(string, start, length)string\uff1a\u5f85\u63d0\u53d6\u5b50\u4e32\u7684\u5b57\u7b26\u4e32start\uff1a\u8d77\u59cb\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\uff09length\uff1a\u5b50\u4e32\u957f\u5ea6 \u5982\u4f55\u4f7f\u7528 SQL \u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5 \u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 SUBSTRING \u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u6bb5\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a SUBSTRING(string, start, length) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a string\uff1a\u8981\u4ece\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u4e32\u3002 start\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\uff09\u3002 length\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a table_name \u7684\u8868\uff0c\u5176\u4e2d\u6709\u4e00\u5217\u540d\u4e3a column_name\uff0c\u8be5\u5217\u5305\u542b\u4ee5\u4e0b\u5b57\u7b26\u4e32\uff1a ABC123 DEF456 GHI789 \u767b\u5f55\u540e\u590d\u5236 \u8981\u63d0\u53d6 ABC123 \u5b57\u7b26\u4e32\u4e2d\u7684 C \u5b57\u7b26\u53ca\u5176\u540e\u7684\u6240\u6709\u5b57\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT SUBSTRING(column_name, 2, LENGTH(column_name) &#8211; 1) FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u8be5\u67e5\u8be2\u5c06\u8fd4\u56de\u4ee5\u4e0b\u7ed3\u679c\uff1a BC123 EF456 HI789 \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a LENGTH(column_name) [&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-5701","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5701","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=5701"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5701\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}