{"id":347,"date":"2023-09-16T10:44:54","date_gmt":"2023-09-16T02:44:54","guid":{"rendered":""},"modified":"2023-09-16T15:50:11","modified_gmt":"2023-09-16T07:50:11","slug":"sql%e5%a6%82%e4%bd%95%e4%bb%8e%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%aa%e5%8f%96%e6%8c%87%e5%ae%9a%e5%ad%97%e7%ac%a6%ef%bc%88left%e3%80%81mid%e3%80%81right%e4%b8%89%e5%a4%a7%e5%87%bd%e6%95%b0%ef%bc%89","status":"publish","type":"post","link":"https:\/\/mushiming.com\/347.html","title":{"rendered":"SQL\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u622a\u53d6\u6307\u5b9a\u5b57\u7b26\uff08LEFT\u3001MID\u3001RIGHT\u4e09\u5927\u51fd\u6570\uff09"},"content":{"rendered":"

\u76ee\u5f55<\/strong><\/p>\n

1.LEFT\u51fd\u6570<\/p>\n

2.MID\u51fd\u6570<\/p>\n

3.RIGHT\u51fd\u6570<\/p>\n


\n

1.LEFT\u51fd\u6570<\/h1>\n

\uff081\uff09\u8bed\u6cd5\uff1aleft(text,len\uff09\u5373left(\u5b57\u7b26\u4e32\u6587\u672c,\u957f\u5ea6\uff09<\/strong><\/p>\n

\u67e5\u8be2\u8bed\u53e5\nselect [column(s),] left(text,len) \n[from table]<\/code><\/pre>\n
\n

\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n

text\uff1a\u5b57\u7b26\u4e32\uff0c\u53ef\u76f4\u63a5\u5f15\u7528\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4e3a\u5b57\u6bb5\uff1b<\/p>\n

len\uff1a\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4e3a\u6b63\u6574\u6570\uff0c\u82e5\u4e3a0\u6216\u8d1f\u6570\u8fd4\u56de\u4e3a\u7a7a\u503c\u3002<\/p>\n

\u8bf4\u660e\uff1a\u67e5\u8be2\u8bed\u53e5\u4e2d\u7528\u201c[]\u201d\u62ec\u8d77\u6765\u7684\u8868\u793a\u53ef\u9009\u3002\u82e5\u548c\u5176\u5b83\u5b57\u6bb5\u4e00\u8d77\u67e5\u8be2\u65f6\u9700\u8981\u8f93\u5165from\u8bed\u53e5\u6307\u5b9a\u76ee\u6807\u8868\uff0c\u82e5text\u4e3a\u5b57\u6bb5\u65f6\u4e5f\u9700\u8981\u8f93\u5165from\u8bed\u53e5\u6307\u5b9a\u76ee\u6807\u8868\uff0c\u82e5\u53ea\u662f\u7528left\u83b7\u53d6\u6307\u5b9a\u67d0\u5b57\u7b26\u4e32\u622a\u53d6\u65f6\u53ef\u76f4\u63a5\u7701\u7565from\u8bed\u53e5\uff0c\u4f46\u662f\u8fd9\u79cd\u7528\u6cd5\u4e00\u822c\u53ea\u662f\u5728\u8c03\u8bd5\u800c\u4e0d\u5e38\u7528\u4e8e\u5b9e\u9645\u5f53\u4e2d\u3002<\/p>\n<\/blockquote>\n

\uff082\uff09\u7528\u6cd5\uff1a\u622a\u53d6\u5de6\u8fb9\u6307\u5b9a\u957f\u5ea6\u5b57\u7b26\u3002<\/strong><\/p>\n

\uff083\uff09\u5b9e\u4f8b<\/strong><\/p>\n

\u4f8b1\uff1a\u4ece\u201chello world\u201d\u622a\u53d6\u201chello\u201d\u3002<\/p>\n

select left('hello world',5)<\/code><\/pre>\n

\u73b0\u6709\u4e00\u5f20\u5b66\u751f\u8868student\uff0c\u5b66\u53f7id\uff0c\u59d3\u540dname\uff0c\u6027\u522bsex\uff0c\u7c4d\u8d2fnative_place\uff0c<\/span>\u6c11\u65cfnation\uff0c\u51fa\u751f\u65e5\u671fborn\uff0c\u5e74\u9f84age\u3002\u5982\u4e0b\u8868\u6240\u793a\u3002<\/p>\n\n\n\n\n\n\n\n\n\n\n
\n student
\n <\/caption>\n
ID<\/td>\nname<\/span><\/td>\nsex<\/span><\/td>\nnative_place<\/span><\/td>\nnation<\/span><\/td>\nborn<\/span><\/td>\nage<\/span><\/td>\n<\/tr>\n
1001<\/span><\/td>\n\u5f20\u4e09<\/span><\/td>\n\u7537<\/span><\/td>\n\u5e7f\u4e1c\u7701<\/span><\/td>\n\u6c49<\/span><\/td>\n2000\/3\/16<\/span><\/td>\n22<\/span><\/td>\n<\/tr>\n
1002<\/span><\/td>\n\u9648\u4e00<\/span><\/td>\n\u5973<\/span><\/td>\n\u5e7f\u4e1c<\/span><\/td>\n\u58ee<\/span><\/td>\n1998\/3\/15<\/span><\/td>\n24<\/span><\/td>\n<\/tr>\n
1003<\/span><\/td>\n\u738b\u4e94<\/span><\/td>\n\u7537<\/span><\/td>\n\u897f\u85cf<\/span><\/td>\n\u85cf<\/span><\/td>\n2002\/1\/6<\/span><\/td>\n20<\/span><\/td>\n<\/tr>\n
1004<\/span><\/td>\n\u9648\u7ea2<\/span><\/td>\n\u5973<\/span><\/td>\n\u5e7f\u4e1c\u7701<\/span><\/td>\n\u9ece<\/span><\/td>\n2001\/1\/17<\/span><\/td>\n21<\/span><\/td>\n<\/tr>\n
1005<\/span><\/td>\n\u674e\u56db<\/span><\/td>\n\u7537<\/span><\/td>\n\u4e1c\u5317\u7701<\/span><\/td>\n\u6c49<\/span><\/td>\n1999\/3\/16<\/span><\/td>\n23<\/span><\/td>\n<\/tr>\n
1006<\/span><\/td>\n-<\/span><\/td>\n\u5973<\/span><\/td>\n\u5e7f\u4e1c\u7701<\/span><\/td>\n\u9ece<\/span><\/td>\n1998\/5\/6<\/span><\/td>\n24<\/span><\/td>\n<\/tr>\n
1008<\/span><\/td>\n\u9648\u5c0f\u5c0f<\/span><\/td>\n\u5973<\/span><\/td>\n\u5e7f\u4e1c<\/span><\/td>\n\u58ee<\/span><\/td>\n1997\/5\/9<\/span><\/td>\n25<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4f8b2\uff1a\u5c06\u7c4d\u8d2f\u4e2d\u7701\u5b57\u53bb\u6389\uff0c\u622a\u53d6\u524d\u4e24\u4e2a\u5b57\u3002\u540c\u65f6\u83b7\u53d6\u5b66\u751f\u5b66\u53f7\u3001\u59d3\u540d\u3001\u6027\u522b\u548c\u5e74\u9f84\u3002<\/p>\n

select ID,name,sex,left(native_place,2),age\nfrom student<\/code><\/pre>\n
\n

2.MID\u51fd\u6570<\/h1>\n

\uff081\uff09\u8bed\u6cd5\u548c\u7528\u6cd5\uff1a\u6709\u4e24\u79cd\u5f62\u5f0f<\/strong><\/p>\n

#\u2460\u4e24\u4e2a\u53c2\u6570\nmid(text,start)  #\u4ece\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u67d0\u5e8f\u6570\u5f00\u59cb\u83b7\u53d6\u5b57\u7b26\n\n#\u2461\u4e09\u4e2a\u53c2\u6570\nmid(text,start,len) #\u4ece\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u67d0\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u83b7\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26<\/code><\/pre>\n
\n

\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n

text\uff1a\u5b57\u7b26\u4e32\uff0c\u53ef\u76f4\u63a5\u5f15\u7528\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4e3a\u5b57\u6bb5\uff1b<\/p>\n

start\uff1a\u5f00\u59cb\u622a\u53d6\u7684\u5b57\u7b26\u4e32\u4f4d\u7f6e\uff0c\u53ef\u4e3a\u6b63\u8d1f\u6574\u6570\u3002<\/p>\n

len\uff1a\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4e3a\u6b63\u6574\u6570\uff0c\u82e5\u4e3a0\u6216\u8d1f\u6570\u8fd4\u56de\u4e3a\u7a7a\u503c\u3002<\/p>\n

\u8bf4\u660e\uff1aMID\u51fd\u6570\u4e0eSUBSTRING()\u662f\u540c\u4e49\u8bcd\uff0c\u7528\u6cd5\u76f8\u540c\u3002<\/p>\n<\/blockquote>\n

\uff082\uff09\u5b9e\u4f8b<\/strong><\/p>\n

\u4f8b\uff1a\u4ece\u201chello world\u201d\u4e2d\u622a\u53d6\u201cworld\u201d\u5b57\u7b26\u4e32\u3002<\/p>\n

\u5f53start\u4e3a\u6b63\u6570\u65f6\uff0c\u5219\u5f00\u59cb\u5b57\u7b26\u201cw\u201d\u4f4d\u7f6e\u4e3a7\uff0c\u6ce8\u610f\u7684\u662f\u201chello\u201d\u548c\u201cworld\u201d\u4e4b\u95f4\u7684\u7a7a\u5b57\u7b26\u662f\u7b97\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n

#start\u4e3a\u6b63\u6570\n#\u4e24\u4e2a\u4e24\u4e2a\u53c2\u6570\nselect mid('hello world',7)  \n\u6216\nselect mid('hello world'from 7)\n\n#\u4e09\u4e2a\u53c2\u6570\nselect mid('hello world',7,5)\n\u6216\nselect mid('hello world'from 7 for 5)<\/code><\/pre>\n
\n

3.RIGHT\u51fd\u6570<\/h1>\n

\uff081\uff09\u8bed\u6cd5\uff1aright(text,len\uff09\u5373right(\u5b57\u7b26\u4e32\u6587\u672c,\u957f\u5ea6\uff09<\/strong><\/p>\n

\u67e5\u8be2\u8bed\u53e5\nselect [column(s),] right(text,len) \n[from table]<\/code><\/pre>\n
\n

\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n

text\uff1a\u5b57\u7b26\u4e32\uff0c\u53ef\u76f4\u63a5\u5f15\u7528\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4e3a\u5b57\u6bb5\uff1b<\/p>\n

len\uff1a\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4e3a\u6b63\u6574\u6570\uff0c\u82e5\u4e3a0\u6216\u8d1f\u6570\u8fd4\u56de\u4e3a\u7a7a\u503c\u3002<\/p>\n

\u8bf4\u660e\uff1a\u67e5\u8be2\u8bed\u53e5\u4e2d\u7528\u201c[]\u201d\u62ec\u8d77\u6765\u7684\u8868\u793a\u53ef\u9009\u3002\u82e5\u548c\u5176\u5b83\u5b57\u6bb5\u4e00\u8d77\u67e5\u8be2\u65f6\u9700\u8981\u8f93\u5165from\u8bed\u53e5\u6307\u5b9a\u76ee\u6807\u8868\uff0c\u82e5text\u4e3a\u5b57\u6bb5\u65f6\u4e5f\u9700\u8981\u8f93\u5165from\u8bed\u53e5\u6307\u5b9a\u76ee\u6807\u8868\uff0c\u82e5\u53ea\u662f\u7528left\u83b7\u53d6\u6307\u5b9a\u67d0\u5b57\u7b26\u4e32\u622a\u53d6\u65f6\u53ef\u76f4\u63a5\u7701\u7565from\u8bed\u53e5\uff0c\u4f46\u662f\u8fd9\u79cd\u7528\u6cd5\u4e00\u822c\u53ea\u662f\u5728\u8c03\u8bd5\u800c\u4e0d\u5e38\u7528\u4e8e\u5b9e\u9645\u5f53\u4e2d\u3002<\/p>\n<\/blockquote>\n

\uff082\uff09\u7528\u6cd5\uff1a\u622a\u53d6\u53f3\u8fb9\u6307\u5b9a\u957f\u5ea6\u5b57\u7b26\u3002<\/strong><\/p>\n

\uff083\uff09\u5b9e\u4f8b<\/strong><\/p>\n

\u4f8b1\uff1a\u4ece\u201chello world\u201d\u622a\u53d6\u201cworld\u201d\u3002<\/p>\n

select right('hello world',5)<\/code><\/pre>\n
\n

SQL\u7684LEFT\u3001MID\u3001RIGHT\u4e09\u5927\u51fd\u6570\u7528\u6cd5\u4e0eEXCEL\u7684LEFT\u3001MID\u3001RIGHT\u4e09\u5927\u51fd\u6570\u7528\u6cd5\u975e\u5e38\u76f8\u4f3c\uff0c\u53ef\u53c2\u8003\uff1aexcel\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u4e2d\u622a\u53d6\u6307\u5b9a\u5b57\u7b26\uff08LEFT\u3001RIGHR\u3001MID\u4e09\u5927\u51fd\u6570\uff09_\u5c0f\u767d\u4fee\u70bc\u664b\u7ea7\u4e2d\u7684\u535a\u5ba2-CSDN\u535a\u5ba2_excel\u622a\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u90e8\u5206<\/p>\n

SQL\u7684substring\u51fd\u6570\u53ef\u53c2\u8003\uff1aSQL\u7684substring\u51fd\u6570\u53ca\u5176\u7528\u6cd5\u5b9e\u4f8b\u2014\u2014MYSQL\u53e6\u4e00\u79cd\u622a\u53d6\u76ee\u6807\u5b57\u7b26\u7684\u51fd\u6570_\u5c0f\u767d\u4fee\u70bc\u664b\u7ea7\u4e2d\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n","protected":false},"excerpt":{"rendered":"1.LEFT\u51fd\u6570\uff081\uff09\u8bed\u6cd5\uff1aleft(text,len\uff09\u5373left(\u5b57\u7b26\u4e32\u6587\u672c,\u957f\u5ea6\uff09\u67e5\u8be2\u8bed\u53e5select[column(s),]left(text,len)[fromtable]\u53c2\u6570\u8bf4...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/347"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=347"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/347\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}