{"id":5731,"date":"2024-11-14T12:37:28","date_gmt":"2024-11-14T04:37:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5731\/"},"modified":"2024-11-14T12:37:28","modified_gmt":"2024-11-14T04:37:28","slug":"sql%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e7%9c%8b%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5731\/","title":{"rendered":"sql\u5b57\u7b26\u4e32\u600e\u4e48\u770b\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  \u67e5\u770b sql \u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u67e5\u770b\u6570\u636e\u7c7b\u578b\uff08varchar\/char\uff09\uff1b2. \u4f7f\u7528 length() \u51fd\u6570\uff1b3. \u4f7f\u7528 substring() \u51fd\u6570\uff1b4. \u6839\u636e\u7279\u5b9a\u6570\u636e\u5e93\u7cfb\u7edf\u4f7f\u7528\u5176\u4ed6\u51fd\u6570\uff08\u5982 mysql \u7684 strlen()\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053021335225966.jpg\" class=\"aligncenter\" title=\"sql\u5b57\u7b26\u4e32\u600e\u4e48\u770b\u957f\u5ea6\u63d2\u56fe\" alt=\"sql\u5b57\u7b26\u4e32\u600e\u4e48\u770b\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u67e5\u770b SQL \u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u67e5\u770b\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<p>SQL \u5b57\u7b26\u4e32\u7684\u6570\u636e\u7c7b\u578b\u901a\u5e38\u662f VARCHAR(n) \u6216 CHAR(n)\uff0c\u5176\u4e2d n \u8868\u793a\u6700\u5927\u5b57\u7b26\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT * FROM users WHERE name VARCHAR(50);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 LENGTH() \u51fd\u6570<\/strong><\/p>\n<p>LENGTH() \u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT LENGTH(name) FROM users;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 SUBSTRING() \u51fd\u6570<\/strong><\/p>\n<p>SUBSTRING() \u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT SUBSTRING(name, 1, LENGTH(name)) FROM users;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u67d0\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u67e5\u770b\u5b57\u7b26\u4e32\u957f\u5ea6\uff1a<\/p>\n<ul>\n<li> <strong>MySQL\uff1a<\/strong>STRLEN() \u51fd\u6570<\/li>\n<li> <strong>PostgreSQL\uff1a<\/strong>LENGTH() \u51fd\u6570<\/li>\n<li> <strong>Oracle\uff1a<\/strong>LENGTH() \u51fd\u6570<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a &#8220;users&#8221; \u7684\u8868\uff0c\u5176\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a &#8220;name&#8221; \u7684 VARCHAR(50) \u5217\u3002\u8981\u67e5\u770b\u5176\u4e2d\u4e00\u4e2a\u7528\u6237\u7684\u59d3\u540d\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT LENGTH(name) FROM users WHERE id = 1;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5b57\u7b26\u4e32\u600e\u4e48\u770b\u957f\u5ea6\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>\u67e5\u770b sql \u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u67e5\u770b\u6570\u636e\u7c7b\u578b\uff08varchar\/char\uff09\uff1b2. \u4f7f\u7528 length() \u51fd\u6570\uff1b3. \u4f7f\u7528 substring() \u51fd\u6570\uff1b4. \u6839\u636e\u7279\u5b9a\u6570\u636e\u5e93\u7cfb\u7edf\u4f7f\u7528\u5176\u4ed6\u51fd\u6570\uff08\u5982 mysql \u7684 strlen()\uff09\u3002 \u5982\u4f55\u67e5\u770b SQL \u5b57\u7b26\u4e32\u7684\u957f\u5ea6 \u76f4\u63a5\u67e5\u770b\u6570\u636e\u7c7b\u578b SQL \u5b57\u7b26\u4e32\u7684\u6570\u636e\u7c7b\u578b\u901a\u5e38\u662f VARCHAR(n) \u6216 CHAR(n)\uff0c\u5176\u4e2d n \u8868\u793a\u6700\u5927\u5b57\u7b26\u6570\u3002\u4f8b\u5982\uff1a SELECT * FROM users WHERE name VARCHAR(50); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 LENGTH() \u51fd\u6570 LENGTH() \u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\u3002\u4f8b\u5982\uff1a SELECT LENGTH(name) FROM users; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 SUBSTRING() \u51fd\u6570 SUBSTRING() \u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff1a SELECT SUBSTRING(name, 1, LENGTH(name)) FROM users; \u767b\u5f55\u540e\u590d\u5236 [&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-5731","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5731","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=5731"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5731\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}