{"id":6166,"date":"2024-11-14T08:38:31","date_gmt":"2024-11-14T00:38:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6166\/"},"modified":"2024-11-14T08:38:31","modified_gmt":"2024-11-14T00:38:31","slug":"mysql%e5%a6%82%e4%bd%95%e6%8b%bc%e6%8e%a5%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6166\/","title":{"rendered":"mysql\u5982\u4f55\u62fc\u63a5\u5b57\u7b26\u4e32"},"content":{"rendered":"<blockquote><p>\n  mysql \u63d0\u4f9b\u4e86 concat() \u51fd\u6570\u3001|| \u64cd\u4f5c\u7b26\u3001format() \u51fd\u6570\u3001group_concat() \u51fd\u6570\u548c + \u53f7\u6765\u62fc\u63a5\u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061403542517785.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u62fc\u63a5\u5b57\u7b26\u4e32\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u62fc\u63a5\u5b57\u7b26\u4e32\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>MySQL \u63d0\u4f9b\u4e86\u591a\u79cd\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u672c\u6587\u5c06\u4e00\u4e00\u4ecb\u7ecd\u3002<\/p>\n<p><strong>CONCAT() \u51fd\u6570<\/strong><\/p>\n<p>CONCAT() \u51fd\u6570\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CONCAT(str1, str2, ..., strN)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cstr1\u3001str2\u3001&#8230;\u3001strN \u662f\u8981\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>CONCAT('Hello', ' ', 'World!')  -- \u8f93\u51fa\uff1aHello World!<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>|| \u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>|| \u64cd\u4f5c\u7b26\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8fde\u63a5\u5b57\u7b26\u4e32\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>str1 || str2 || ... || strN<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0e CONCAT() \u51fd\u6570\u7c7b\u4f3c\uff0c|| \u64cd\u4f5c\u7b26\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>'Hello' || ' ' || 'World!'  -- \u8f93\u51fa\uff1aHello World!<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>format() \u51fd\u6570<\/strong><\/p>\n<p>format() \u51fd\u6570\u53ef\u4ee5\u5c06\u53d8\u91cf\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u5e76\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>format(format_string, arg1, arg2, ..., argN)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cformat_string \u662f\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5305\u542b\u5360\u4f4d\u7b26 %s\u3001%d \u7b49\u3002arg1\u3001arg2\u3001&#8230;\u3001argN \u662f\u8981\u683c\u5f0f\u5316\u5e76\u8fde\u63a5\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT format('Name: %s, Age: %d', 'John', 30);  -- \u8f93\u51fa\uff1aName: John, Age: 30<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>GROUP_CONCAT() \u51fd\u6570<\/strong><\/p>\n<p>GROUP_CONCAT() \u51fd\u6570\u7528\u4e8e\u5c06\u4e00\u7ec4\u884c\u4e2d\u7684\u67d0\u4e2a\u5217\u7684\u503c\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>GROUP_CONCAT(column_name)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0ccolumn_name \u662f\u8981\u8fde\u63a5\u7684\u5217\u540d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT GROUP_CONCAT(name) FROM users;  -- \u8f93\u51fa\uff1aJohn, Mary, Bob<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 + \u53f7<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 + \u53f7\u4e5f\u53ef\u4ee5\u8fde\u63a5\u5b57\u7b26\u4e32\u3002\u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u5176\u4e2d\u4e00\u4e2a\u64cd\u4f5c\u6570\u4e3a\u6570\u5b57\u65f6\uff0c+ \u53f7\u5c06\u6267\u884c\u52a0\u6cd5\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>1 + 'Hello'  -- \u8f93\u51fa\uff1a1Hello<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u662f MySQL \u4e2d\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u7528\u4f8b\u548c\u6027\u80fd\u8981\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u62fc\u63a5\u5b57\u7b26\u4e32\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>mysql \u63d0\u4f9b\u4e86 concat() \u51fd\u6570\u3001|| \u64cd\u4f5c\u7b26\u3001format() \u51fd\u6570\u3001group_concat() \u51fd\u6570\u548c + \u53f7\u6765\u62fc\u63a5\u5b57\u7b26\u4e32\u3002 MySQL \u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5 MySQL \u63d0\u4f9b\u4e86\u591a\u79cd\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u672c\u6587\u5c06\u4e00\u4e00\u4ecb\u7ecd\u3002 CONCAT() \u51fd\u6570 CONCAT() \u51fd\u6570\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a CONCAT(str1, str2, &#8230;, strN) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cstr1\u3001str2\u3001&#8230;\u3001strN \u662f\u8981\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a CONCAT(&#8216;Hello&#8217;, &#8216; &#8216;, &#8216;World!&#8217;) &#8212; \u8f93\u51fa\uff1aHello World! \u767b\u5f55\u540e\u590d\u5236 || \u64cd\u4f5c\u7b26 || \u64cd\u4f5c\u7b26\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8fde\u63a5\u5b57\u7b26\u4e32\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a str1 || str2 || &#8230; || strN \u767b\u5f55\u540e\u590d\u5236 \u4e0e CONCAT() \u51fd\u6570\u7c7b\u4f3c\uff0c|| \u64cd\u4f5c\u7b26\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u4f8b\u5982\uff1a &#8216;Hello&#8217; || &#8216; &#8216; || &#8216;World!&#8217; &#8212; [&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-6166","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6166","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=6166"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6166\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}