{"id":1739,"date":"2025-02-16T09:10:06","date_gmt":"2025-02-16T01:10:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1739"},"modified":"2025-02-16T09:10:06","modified_gmt":"2025-02-16T01:10:06","slug":"sql-mid-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/sql-mid-%e5%87%bd%e6%95%b0\/","title":{"rendered":"SQL MID() \u51fd\u6570\u00a0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>MID()<\/code> \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u6307\u5b9a\u4f4d\u7f6e\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u622a\u53d6\u6307\u5b9a\u4f4d\u7f6e\u5f00\u59cb\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>MID()<\/code> \u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>MID(string, start_position, length);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>string<\/code>\uff1a\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u4ece\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><code>start_position<\/code>\uff1a\u5f00\u59cb\u63d0\u53d6\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u7d22\u5f15\u4ece 1 \u5f00\u59cb\u3002<\/li>\n\n\n\n<li><code>length<\/code>\uff1a\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u6307\u5b9a\u4ece\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u63d0\u53d6\u591a\u5c11\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u7701\u7565 <code>length<\/code>\uff0c\u5219\u4f1a\u63d0\u53d6\u4ece <code>start_position<\/code> \u5f00\u59cb\u7684\u6240\u6709\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>MID()<\/code> \u7684\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a <code>employees<\/code> \u7684\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Alice Johnson<\/td><\/tr><tr><td>2<\/td><td>Bob Smith<\/td><\/tr><tr><td>3<\/td><td>Charlie Brown<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u4ece <code>emp_name<\/code> \u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u4ece <code>emp_name<\/code> \u4e2d\u63d0\u53d6\u4ece\u7b2c 1 \u4e2a\u5b57\u7b26\u5f00\u59cb\u7684 5 \u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u67e5\u8be2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_id, MID(emp_name, 1, 5) AS emp_name_substring\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name_substring<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Alice<\/td><\/tr><tr><td>2<\/td><td>Bob S<\/td><\/tr><tr><td>3<\/td><td>Charl<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MID(emp_name, 1, 5)<\/code> \u8868\u793a\u4ece <code>emp_name<\/code> \u5b57\u6bb5\u7684\u7b2c 1 \u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u63d0\u53d6 5 \u4e2a\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u4ece <code>emp_name<\/code> \u4e2d\u63d0\u53d6\u6240\u6709\u5b57\u7b26\uff08\u6ca1\u6709 <code>length<\/code> \u53c2\u6570\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4e0d\u6307\u5b9a <code>length<\/code>\uff0c<code>MID()<\/code> \u4f1a\u63d0\u53d6\u4ece <code>start_position<\/code> \u5f00\u59cb\u7684\u6240\u6709\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u4ece\u7b2c 7 \u4e2a\u5b57\u7b26\u5f00\u59cb\u63d0\u53d6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_id, MID(emp_name, 7) AS emp_name_substring\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name_substring<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Johnson<\/td><\/tr><tr><td>2<\/td><td>Smith<\/td><\/tr><tr><td>3<\/td><td>Brown<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MID(emp_name, 7)<\/code> \u8868\u793a\u4ece <code>emp_name<\/code> \u7684\u7b2c 7 \u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u63d0\u53d6\u8be5\u4f4d\u7f6e\u540e\u7684\u6240\u6709\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>MID()<\/code> \u4e0e <code>SUBSTRING()<\/code> \u51fd\u6570\u7684\u5bf9\u6bd4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8bb8\u591a\u6570\u636e\u5e93\u4e2d\uff0c<code>MID()<\/code> \u548c <code>SUBSTRING()<\/code> \u7684\u529f\u80fd\u662f\u76f8\u540c\u7684\uff0c\u90fd\u662f\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e92\u6362\u4f7f\u7528\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>MID()<\/code><\/strong>\uff1a\u901a\u5e38\u5728 MySQL \u548c\u5176\u4ed6\u4e00\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong><code>SUBSTRING()<\/code><\/strong>\uff1a\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u5728\u5176\u4ed6\u6570\u636e\u5e93\u7cfb\u7edf\uff08\u5982 SQL Server\u3001PostgreSQL \u7b49\uff09\u4e2d\u66f4\u4e3a\u5e38\u89c1\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4e24\u4e2a\u67e5\u8be2\u662f\u7b49\u4ef7\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MID(emp_name, 1, 5) AS emp_name_substring\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u548c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SUBSTRING(emp_name, 1, 5) AS emp_name_substring\nFROM employees;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong><code>MID()<\/code> \u51fd\u6570\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong>\u8d1f\u6570\u7684 <code>start_position<\/code><\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>start_position<\/code> \u662f\u8d1f\u6570\uff0c\u5927\u90e8\u5206 SQL \u5b9e\u73b0\uff08\u5982 MySQL\uff09\u4f1a\u4ece\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u5f00\u59cb\u8ba1\u7b97\u4f4d\u7f6e\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MID(emp_name, -6, 5) AS emp_name_substring\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\uff0c<code>-6<\/code> \u8868\u793a\u4ece\u5b57\u7b26\u4e32\u7684\u5012\u6570\u7b2c 6 \u4e2a\u5b57\u7b26\u5f00\u59cb\u63d0\u53d6\uff0c\u63d0\u53d6 5 \u4e2a\u5b57\u7b26\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name_substring<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>John<\/td><\/tr><tr><td>2<\/td><td>Smith<\/td><\/tr><tr><td>3<\/td><td>Brown<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong>\u8d85\u51fa\u5b57\u7b26\u4e32\u957f\u5ea6\u7684 <code>length<\/code> \u53c2\u6570<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>length<\/code> \u53c2\u6570\u5927\u4e8e\u5b57\u7b26\u4e32\u7684\u5269\u4f59\u5b57\u7b26\u6570\uff0c<code>MID()<\/code> \u4f1a\u8fd4\u56de\u4ece <code>start_position<\/code> \u5f00\u59cb\u7684\u6240\u6709\u5269\u4f59\u5b57\u7b26\uff0c\u800c\u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MID(emp_name, 1, 20) AS emp_name_substring\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e <code>emp_name<\/code> \u4e3a <code>\"Alice Johnson\"<\/code> \u7684\u6570\u636e\uff0c<code>MID()<\/code> \u4f1a\u8fd4\u56de <code>\"Alice Johnson\"<\/code>\uff0c\u5373\u4f7f\u4f60\u8bf7\u6c42\u63d0\u53d6 20 \u4e2a\u5b57\u7b26\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u603b\u957f\u5ea6\u4e0d\u8db3 20 \u4e2a\u5b57\u7b26\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 <strong>\u7a7a\u5b57\u7b26\u4e32\u6216 <code>NULL<\/code> \u503c\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>string<\/code> \u53c2\u6570\u4e3a <code>NULL<\/code> \u6216\u7a7a\u5b57\u7b26\u4e32\uff0c<code>MID()<\/code> \u51fd\u6570\u5c06\u8fd4\u56de <code>NULL<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MID(NULL, 1, 5);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>MID(NULL, 1, 5)<\/th><\/tr><\/thead><tbody><tr><td>NULL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">4.4 <strong>\u4f4d\u7f6e\u8d8a\u754c\u95ee\u9898<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u4fdd <code>start_position<\/code> \u53c2\u6570\u4e0d\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u5b9e\u9645\u957f\u5ea6\u3002\u5982\u679c <code>start_position<\/code> \u8d85\u8fc7\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c<code>MID()<\/code> \u51fd\u6570\u5c06\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u6216 <code>NULL<\/code>\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MID('Hello', 10, 5);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u8fd9\u6761\u67e5\u8be2\uff0c\u7531\u4e8e\u8d77\u59cb\u4f4d\u7f6e\u8d85\u8fc7\u4e86\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u7ed3\u679c\u5c06\u662f\u7a7a\u5b57\u7b26\u4e32\u6216 <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MID()<\/code> \u51fd\u6570\u7528\u4e8e\u4ece\u6307\u5b9a\u4f4d\u7f6e\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a <code>start_position<\/code> \u548c <code>length<\/code> \u6765\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\uff0c\u6216\u4ec5\u4f7f\u7528 <code>start_position<\/code> \u6765\u63d0\u53d6\u4ece\u8be5\u4f4d\u7f6e\u5f00\u59cb\u7684\u6240\u6709\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><code>MID()<\/code> \u4e0e <code>SUBSTRING()<\/code> \u51fd\u6570\u5728\u5927\u591a\u6570\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u662f\u7b49\u4ef7\u7684\u3002<\/li>\n\n\n\n<li>\u5904\u7406\u8d1f\u6570\u4f4d\u7f6e\u3001\u8d85\u957f <code>length<\/code> \u53c2\u6570\u548c\u7a7a\u5b57\u7b26\u4e32\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>MID() \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u6307\u5b9a\u4f4d\u7f6e\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u622a\u53d6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1739","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1739"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1739\/revisions"}],"predecessor-version":[{"id":1740,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1739\/revisions\/1740"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}