{"id":4522,"date":"2019-10-13T17:32:39","date_gmt":"2019-10-13T09:32:39","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4522"},"modified":"2019-10-13T17:32:39","modified_gmt":"2019-10-13T09:32:39","slug":"sql-%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a4%9a%e8%a1%8c%e6%8b%bc%e6%8e%a5-group_concat-vs-string_agg","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4522","title":{"rendered":"SQL \u4e2d\u5b57\u7b26\u4e32\u591a\u884c\u62fc\u63a5 group_concat VS string_agg"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u591a\u884c\u5b57\u7b26\u7684\u62fc\u63a5\u5f88\u5e38\u7528\u3002\u5728 MySQL \u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38 group_concat \u51fd\u6570\uff0c\u6839\u636e\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u5b57\uff0c\u6211\u4eec\u5c31\u5e94\u8be5\u80fd\u731c\u5230\u5b83\u7684\u4f5c\u7528\uff0c\u5206\u7ec4\u540e\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u4e0b\u9762\u4e00\u4e2a\u573a\u666f\uff0c\u5c0f\u7c73\u540c\u5b66\u5728\u4e00\u5929\u5185\u5e72\u4e86\u5982\u4e0b\u4e8b\u60c5\uff1a<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>\u59d3\u540d<\/td><td>\u5185\u5bb9<\/td><\/tr><tr><td>\u5c0f\u7c73<\/td><td>\u770b\u7535\u89c6<\/td><\/tr><tr><td>\u5c0f\u660e<\/td><td>\u6253\u6e38\u620f<\/td><\/tr><tr><td>\u5c0f\u7c73<\/td><td>\u5b66\u4e60\u6781\u5ba2\u65f6\u95f4<\/td><\/tr><tr><td>\u5c0f\u738b<\/td><td>\u64a9\u59b9<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u73b0\u5728\u8981\u7edf\u8ba1\u51fa\uff0c\u4e0b\u9762\u7684\u6548\u679c\uff1a<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>\u59d3\u540d<\/td><td>\u5185\u5bb9<\/td><\/tr><tr><td>\u5c0f\u7c73<\/td><td>\u770b\u7535\u89c6,\u5b66\u4e60\u6781\u5ba2\u65f6\u95f4<\/td><\/tr><tr><td>\u5c0f\u660e<\/td><td>\u6253\u6e38\u620f<\/td><\/tr><tr><td>\u5c0f\u738b<\/td><td>\u64a9\u59b9<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u65f6 MySQL \u4e2d\u7684 group_concat \u51fd\u6570\u5c31\u53ef\u4ee5\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name,group_concat(context,',') \nFROM student GROUP BY name;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d\uff0cname \u548c context \u5206\u522b\u4ee3\u8868\u4e0a\u9762\u7684\u59d3\u540d\u548c\u5185\u5bb9\u5b57\u6bb5\u3002group_concat \u51fd\u6570\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5c06 group by \u4ea7\u751f\u7684\u540c\u4e00\u4e2a\u5206\u7ec4\u4e2d\u7684\u503c\u8fde\u63a5\u8d77\u6765\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u7ed3\u679c\u3002\u8bed\u6cd5\uff1agroup_concat( [distinct] \u8981\u8fde\u63a5\u7684\u5b57\u6bb5 [order by \u6392\u5e8f\u5b57\u6bb5 asc\/desc ] [separator &#8216;\u5206\u9694\u7b26&#8217;] )\u3002\u6839\u636e\u8fd9\u4e2a\u8bed\u6cd5\uff0c\u6211\u4eec\u77e5\u9053\uff0cgroup_concat \u51fd\u6570\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528distinct \u8fdb\u884c\u6392\u9664\u91cd\u590d\u503c\uff1b\u5982\u679c\u5e0c\u671b\u5bf9\u7ed3\u679c\u4e2d\u7684\u503c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528order by\u5b50\u53e5\uff1bseparator\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\uff0c\u7f3a\u7701\u4e3a\u4e00\u4e2a\u9017\u53f7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\uff0c\u4eca\u5929\u6709\u4e00\u4e2a\u7f51\u53cb\u95ee\u6211\uff0c\u5728 PostgreSQL \u4e2d\u6709\u6ca1\u6709\u8fd9\u6837\u7684\u51fd\u6570\u5462\uff1f\u8bf4\u5b9e\u8bdd\uff0c\u6211\u4e5f\u4e0d\u592a\u786e\u5b9a\uff0c\u4e8e\u662f\u6211\u4e0a\u5b98\u7f51\u67e5\u4e86\u4e00\u4e0b\u6587\u6863\uff0c\u53d1\u73b0\u6709\u4e00\u4e2a\u53eb string_agg \u7684\u51fd\u6570\u5177\u6709\u8fd9\u6837\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdn.net\/20180910210714336\" alt=\"\" \/><figcaption>Postgresql \u5e38\u7528\u51fd\u6570\u5217\u8868<\/figcaption><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>select name,\nstring_agg(context,',') as contexts\nfrom student\ngroup by name;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cstring_agg \u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u5728 postgres 9.0 \u4ee5\u4e0a\u7684\u7248\u672c\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a postgres 9.0 \u4e00\u4e0b\u7684\u7248\u672c\u4e2d\uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u4e00\u4e2a\u51fd\u6570\uff0c\u9700\u8981\u6211\u4eec\u81ea\u5df1\u53bb\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\u662f\uff1a\u4f7f\u7528 arr_agg \u7684\u51fd\u6570\u6765\u5b9e\u73b0\u805a\u5408\uff0c\u4e0d\u8fc7\u8fd4\u56de\u7684\u7c7b\u578b\u662f Array\uff0c\u7136\u540e\u6211\u4eec\u518d\u901a\u8fc7 array_to_string(array_agg(\u5b57\u6bb5),&#8217;,&#8217;) \u51fd\u6570\u628a\u6570\u7ec4\u8f6c\u5316\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u662f\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u5b9e\u73b0\u4e00\u4e2a\u7c7b string_agg \u6216 group_concat \u51fd\u6570\uff0c\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE AGGREGATE GROUP_CONCAT (anyelement)\n(\n\u00a0 \u00a0 sfunc = array_append, -- \u6bcf\u884c\u7684\u64cd\u4f5c\u51fd\u6570\uff0c\u5c06\u672c\u884cappend\u5230\u6570\u7ec4\u91cc\u00a0\n\u00a0 \u00a0 stype = anyarray, \u00a0-- \u805a\u96c6\u540e\u8fd4\u56de\u6570\u7ec4\u7c7b\u578b\u00a0\n\u00a0 \u00a0 initcond = '{}' \u00a0 \u00a0-- \u521d\u59cb\u5316\u7a7a\u6570\u7ec4\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u5c31\u6a21\u62df\u4e00\u4e2a\u7c7b\u4f3c mysql \u5185\u90e8 GROUP_CONCAT \u7684\u51fd\u6570\u3002\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \u00a0name,array_to_string(GROUP_CONCAT(context),',') \u00a0AS \"contexts\"\nFROM student group by name;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\uff0c\u7b80\u5355\u5217\u4e3e\u4e86 group_concat \u548c string_agg \u5404\u81ea\u7684\u7528\u6cd5\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u5230\u66f4\u591a\u7684\u7a0b\u5e8f\u5458\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4522\">SQL \u4e2d\u5b57\u7b26\u4e32\u591a\u884c\u62fc\u63a5 group_concat VS string_agg<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u591a\u884c\u5b57\u7b26\u7684\u62fc\u63a5\u5f88\u5e38\u7528\u3002\u5728 MySQL \u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38 group_concat \u51fd\u6570\uff0c\u6839\u636e\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u5b57\uff0c\u6211\u4eec\u5c31 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[2023,2021,83,429,2022],"class_list":["post-4522","post","type-post","status-publish","format-standard","hentry","category-sql","tag-array_to_string","tag-group_concat","tag-mysql","tag-postgresql","tag-string_agg"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4522"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4522\/revisions"}],"predecessor-version":[{"id":4523,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4522\/revisions\/4523"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}