{"id":187786,"date":"2024-11-11T17:10:21","date_gmt":"2024-11-11T09:10:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/187786\/"},"modified":"2024-11-11T17:10:22","modified_gmt":"2024-11-11T09:10:22","slug":"%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e5%88%86%e7%b5%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/187786\/","title":{"rendered":"\u6578\u64da\u5eab\u4e2d\u5206\u7d44\u5b57\u7b26\u4e32\u76f8\u52a0"},"content":{"rendered":"<h1 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e5%88%86%e7%b5%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e5%8a%a0-jGEbRVhopz\">\u6578\u64da\u5eab\u4e2d\u5206\u7d44\u5b57\u7b26\u4e32\u76f8\u52a0<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0c\u5b57\u7b26\u4e32\u7684\u8655\u7406\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5c0d\u6578\u64da\u9032\u884c\u5206\u7d44\u548c\u805a\u5408\u7684\u60c5\u6cc1\u4e0b\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728\u6578\u64da\u5eab\u4e2d\u5c0d\u5206\u7d44\u7684\u5b57\u7b26\u4e32\u9032\u884c\u76f8\u52a0\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\uff0c\u4ee5\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e5%8a%a0%ef%bc%9f-jGEbRVhopz\">\u4ec0\u9ebc\u662f\u5b57\u7b26\u4e32\u76f8\u52a0\uff1f<\/h2>\n<p>\u5b57\u7b26\u4e32\u76f8\u52a0\uff0c\u7c21\u55ae\u4f86\u8aaa\uff0c\u5c31\u662f\u5c07\u591a\u500b\u5b57\u7b26\u4e32\u5408\u4f75\u6210\u4e00\u500b\u5b57\u7b26\u4e32\u3002\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u9019\u901a\u5e38\u6d89\u53ca\u5230\u5c0d\u67d0\u4e00\u5217\u7684\u6578\u64da\u9032\u884c\u805a\u5408\uff0c\u7279\u5225\u662f\u5728\u4f7f\u7528 SQL \u67e5\u8a62\u6642\u3002\u9019\u7a2e\u64cd\u4f5c\u5728\u5831\u544a\u751f\u6210\u3001\u6578\u64da\u5206\u6790\u548c\u6578\u64da\u6e05\u7406\u7b49\u5834\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-sql-%e9%80%b2%e8%a1%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e5%8a%a0-jGEbRVhopz\">\u4f7f\u7528 SQL \u9032\u884c\u5b57\u7b26\u4e32\u76f8\u52a0<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c\u5b57\u7b26\u4e32\u7684\u76f8\u52a0\u901a\u5e38\u4f7f\u7528 <code>GROUP_CONCAT<\/code> \u51fd\u6578\u4f86\u5be6\u73fe\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u5c07\u540c\u4e00\u7d44\u4e2d\u7684\u591a\u500b\u5b57\u7b26\u4e32\u5408\u4f75\u70ba\u4e00\u500b\u5b57\u7b26\u4e32\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u5206\u9694\u7b26\u3002\u4ee5\u4e0b\u662f <code>GROUP_CONCAT<\/code> \u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>SELECT GROUP_CONCAT(column_name SEPARATOR 'separator') \nFROM table_name \nWHERE condition \nGROUP BY group_column;<\/code><\/pre>\n<h3 id=\"%e7%a4%ba%e4%be%8b-jGEbRVhopz\">\u793a\u4f8b<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>orders<\/code> \u7684\u8868\uff0c\u8a72\u8868\u5305\u542b\u4ee5\u4e0b\u6578\u64da\uff1a<\/p>\n<pre><code>+----+------------+----------+\n| id | customer   | product  |\n+----+------------+----------+\n| 1  | Alice      | Apple    |\n| 2  | Bob        | Banana   |\n| 3  | Alice      | Orange   |\n| 4  | Bob        | Grape    |\n+----+------------+----------+<\/code><\/pre>\n<p>\u5982\u679c\u6211\u5011\u60f3\u8981\u6309\u5ba2\u6236\u5206\u7d44\uff0c\u4e26\u5c07\u6bcf\u4f4d\u5ba2\u6236\u6240\u8cfc\u8cb7\u7684\u7522\u54c1\u5408\u4f75\u70ba\u4e00\u500b\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT customer, GROUP_CONCAT(product SEPARATOR ', ') AS products \nFROM orders \nGROUP BY customer;<\/code><\/pre>\n<p>\u57f7\u884c\u4e0a\u8ff0\u67e5\u8a62\u5f8c\uff0c\u5c07\u5f97\u5230\u4ee5\u4e0b\u7d50\u679c\uff1a<\/p>\n<pre><code>+----------+---------------------+\n| customer | products            |\n+----------+---------------------+\n| Alice    | Apple, Orange       |\n| Bob      | Banana, Grape       |\n+----------+---------------------+<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-jGEbRVhopz\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528 <code>GROUP_CONCAT<\/code> \u6642\uff0c\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805\u9700\u8981\u8003\u616e\uff1a<\/p>\n<ul>\n<li><strong>\u6700\u5927\u9577\u5ea6\uff1a<\/strong>\u9ed8\u8a8d\u60c5\u6cc1\u4e0b\uff0c<code>GROUP_CONCAT<\/code> \u7684\u7d50\u679c\u9577\u5ea6\u662f\u6709\u9650\u5236\u7684\uff0c\u901a\u5e38\u70ba 1024 \u5b57\u7b26\u3002\u5982\u679c\u9700\u8981\u66f4\u9577\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e <code>group_concat_max_len<\/code> \u8b8a\u91cf\u4f86\u8abf\u6574\u3002<\/li>\n<li><strong>\u5206\u9694\u7b26\uff1a<\/strong>\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u5206\u9694\u7b26\uff0c\u9019\u5c0d\u65bc\u751f\u6210\u53ef\u8b80\u6027\u66f4\u9ad8\u7684\u5b57\u7b26\u4e32\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/li>\n<li><strong>NULL \u503c\uff1a<\/strong>\u5982\u679c\u67d0\u4e00\u884c\u7684\u503c\u70ba NULL\uff0c\u5247\u8a72\u884c\u4e0d\u6703\u88ab\u5305\u542b\u5728\u7d50\u679c\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%85%b6%e4%bb%96%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%af%a6%e7%8f%be-jGEbRVhopz\">\u5176\u4ed6\u6578\u64da\u5eab\u7684\u5be6\u73fe<\/h2>\n<p>\u96d6\u7136 <code>GROUP_CONCAT<\/code> \u662f MySQL \u4e2d\u7684\u5c08\u6709\u51fd\u6578\uff0c\u4f46\u5176\u4ed6\u6578\u64da\u5eab\u7cfb\u7d71\u4e5f\u63d0\u4f9b\u4e86\u985e\u4f3c\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u5728 PostgreSQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>STRING_AGG<\/code> \u51fd\u6578\uff0c\u800c\u5728 SQL Server \u4e2d\u5247\u53ef\u4ee5\u4f7f\u7528 <code>FOR XML PATH<\/code> \u4f86\u5be6\u73fe\u5b57\u7b26\u4e32\u7684\u805a\u5408\u3002<\/p>\n<h3 id=\"postgresql-%e7%a4%ba%e4%be%8b-jGEbRVhopz\">PostgreSQL \u793a\u4f8b<\/h3>\n<pre><code>SELECT customer, STRING_AGG(product, ', ') AS products \nFROM orders \nGROUP BY customer;<\/code><\/pre>\n<h3 id=\"sql-server-%e7%a4%ba%e4%be%8b-jGEbRVhopz\">SQL Server \u793a\u4f8b<\/h3>\n<pre><code>SELECT customer, \n       STUFF((SELECT ', ' + product \n              FROM orders o2 \n              WHERE o1.customer = o2.customer \n              FOR XML PATH('')), 1, 2, '') AS products \nFROM orders o1 \nGROUP BY customer;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-jGEbRVhopz\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\u5c0d\u5206\u7d44\u5b57\u7b26\u4e32\u9032\u884c\u76f8\u52a0\u662f\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e6b\u52a9\u7528\u6236\u66f4\u597d\u5730\u5206\u6790\u548c\u5831\u544a\u6578\u64da\u3002\u7121\u8ad6\u662f\u4f7f\u7528 MySQL \u7684 <code>GROUP_CONCAT<\/code>\uff0c\u9084\u662f\u5176\u4ed6\u6578\u64da\u5eab\u7684\u985e\u4f3c\u51fd\u6578\uff0c\u9019\u4e9b\u6280\u8853\u90fd\u80fd\u6709\u6548\u5730\u5c07\u591a\u500b\u5b57\u7b26\u4e32\u5408\u4f75\u70ba\u4e00\u500b\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u6578\u64da\u5eab\u7ba1\u7406\u548c <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5728\u6578\u64da\u5eab\u4e2d\u5c0d\u5206\u7d44\u5b57\u7b26\u4e32\u9032\u884c\u76f8\u52a0\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u67e5\u8a62\u64cd\u4f5c\uff0c\u512a\u5316\u6578\u64da\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-187786","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=187786"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187786\/revisions"}],"predecessor-version":[{"id":187787,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187786\/revisions\/187787"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=187786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=187786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=187786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}