{"id":180702,"date":"2024-11-09T12:28:19","date_gmt":"2024-11-09T04:28:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/180702\/"},"modified":"2024-11-09T12:28:20","modified_gmt":"2024-11-09T04:28:20","slug":"%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3mysql%e4%b8%ad%e7%9a%84json_arrayagg%e5%92%8cjson_object%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/180702\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3MySQL\u4e2d\u7684JSON_ARRAYAGG\u548cJSON_OBJECT\u51fd\u6578"},"content":{"rendered":"<h1 id=\"%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3mysql%e4%b8%ad%e7%9a%84json_arrayagg%e5%92%8cjson_object%e5%87%bd%e6%95%b8-sAQRDSewMp\">\u6df1\u5165\u4e86\u89e3MySQL\u4e2d\u7684JSON_ARRAYAGG\u548cJSON_OBJECT\u51fd\u6578<\/h1>\n<p>\u96a8\u8457\u6578\u64da\u5eab\u6280\u8853\u7684\u767c\u5c55\uff0cMySQL\u5728\u8655\u7406JSON\u6578\u64da\u65b9\u9762\u7684\u80fd\u529b\u8d8a\u4f86\u8d8a\u5f37\u5927\u3002\u7279\u5225\u662f\uff0c<code>JSON_ARRAYAGG<\/code>\u548c<code>JSON_OBJECT<\/code>\u9019\u5169\u500b\u51fd\u6578\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u4f86\u751f\u6210\u548c\u64cd\u4f5cJSON\u6578\u64da\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u5169\u500b\u51fd\u6578\u7684\u7528\u6cd5\u53ca\u5176\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u7684\u91cd\u8981\u6027\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afjson_arrayagg%e5%87%bd%e6%95%b8%ef%bc%9f-sAQRDSewMp\">\u4ec0\u9ebc\u662fJSON_ARRAYAGG\u51fd\u6578\uff1f<\/h2>\n<p><code>JSON_ARRAYAGG<\/code>\u51fd\u6578\u7528\u65bc\u5c07\u591a\u884c\u6578\u64da\u805a\u5408\u6210\u4e00\u500bJSON\u6578\u7d44\u3002\u9019\u5728\u9700\u8981\u5c07\u67e5\u8a62\u7d50\u679c\u8f49\u63db\u70baJSON\u683c\u5f0f\u6642\u975e\u5e38\u6709\u7528\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u591a\u5c0d\u4e00\u7684\u95dc\u4fc2\u6642\u3002<\/p>\n<h3 id=\"%e8%aa%9e%e6%b3%95-sAQRDSewMp\">\u8a9e\u6cd5<\/h3>\n<pre><code>JSON_ARRAYAGG(value)\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>value<\/code>\u662f\u8981\u805a\u5408\u7684\u5217\u6216\u8868\u9054\u5f0f\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b-sAQRDSewMp\">\u793a\u4f8b<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba<code>orders<\/code>\u7684\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u6578\u64da\uff1a<\/p>\n<pre><code>+----+------------+-------+\n| id | customer_id| amount|\n+----+------------+-------+\n| 1  | 1          | 100   |\n| 2  | 1          | 150   |\n| 3  | 2          | 200   |\n+----+------------+-------+\n<\/code><\/pre>\n<p>\u5982\u679c\u6211\u5011\u60f3\u8981\u7372\u53d6\u6bcf\u500b\u5ba2\u6236\u7684\u6240\u6709\u8a02\u55ae\u91d1\u984d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT customer_id, JSON_ARRAYAGG(amount) AS order_amounts\nFROM orders\nGROUP BY customer_id;\n<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u6bcf\u500b\u5ba2\u6236\u7684\u8a02\u55ae\u91d1\u984d\u7684JSON\u6578\u7d44\uff1a<\/p>\n<pre><code>+------------+-------------------+\n| customer_id| order_amounts     |\n+------------+-------------------+\n| 1          | [100, 150]        |\n| 2          | [200]             |\n+------------+-------------------+\n<\/code><\/pre>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afjson_object%e5%87%bd%e6%95%b8%ef%bc%9f-sAQRDSewMp\">\u4ec0\u9ebc\u662fJSON_OBJECT\u51fd\u6578\uff1f<\/h2>\n<p><code>JSON_OBJECT<\/code>\u51fd\u6578\u7528\u65bc\u5275\u5efa\u4e00\u500bJSON\u5c0d\u8c61\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5c07\u591a\u500b\u5217\u7684\u6578\u64da\u7d44\u5408\u6210\u4e00\u500bJSON\u683c\u5f0f\u7684\u7d50\u679c\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h3 id=\"%e8%aa%9e%e6%b3%95-sAQRDSewMp\">\u8a9e\u6cd5<\/h3>\n<pre><code>JSON_OBJECT(key1, value1, key2, value2, ...)\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>key<\/code>\u662fJSON\u5c0d\u8c61\u4e2d\u7684\u9375\uff0c<code>value<\/code>\u662f\u5c0d\u61c9\u7684\u503c\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b-sAQRDSewMp\">\u793a\u4f8b<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba<code>products<\/code>\u7684\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u6578\u64da\uff1a<\/p>\n<pre><code>+----+----------+-------+\n| id | name     | price |\n+----+----------+-------+\n| 1  | Apple    | 1.00  |\n| 2  | Banana   | 0.50  |\n| 3  | Cherry   | 2.00  |\n+----+----------+-------+\n<\/code><\/pre>\n<p>\u5982\u679c\u6211\u5011\u60f3\u8981\u5c07\u6bcf\u500b\u7522\u54c1\u7684\u540d\u7a31\u548c\u50f9\u683c\u7d44\u5408\u6210\u4e00\u500bJSON\u5c0d\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT JSON_OBJECT('name', name, 'price', price) AS product_info\nFROM products;\n<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u6bcf\u500b\u7522\u54c1\u7684JSON\u5c0d\u8c61\uff1a<\/p>\n<pre><code>+---------------------+\n| product_info        |\n+---------------------+\n| {\"name\": \"Apple\", \"price\": 1.00}   |\n| {\"name\": \"Banana\", \"price\": 0.50}  |\n| {\"name\": \"Cherry\", \"price\": 2.00}  |\n+---------------------+\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e5%90%88%e4%bd%bf%e7%94%a8json_arrayagg%e5%92%8cjson_object-sAQRDSewMp\">\u7d50\u5408\u4f7f\u7528JSON_ARRAYAGG\u548cJSON_OBJECT<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c<code>JSON_ARRAYAGG<\/code>\u548c<code>JSON_OBJECT<\/code>\u53ef\u4ee5\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u751f\u6210\u66f4\u8907\u96dc\u7684JSON\u7d50\u69cb\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u60f3\u8981\u70ba\u6bcf\u500b\u5ba2\u6236\u751f\u6210\u4e00\u500b\u5305\u542b\u5176\u6240\u6709\u8a02\u55ae\u7684JSON\u5c0d\u8c61\uff0c\u53ef\u4ee5\u9019\u6a23\u505a\uff1a<\/p>\n<pre><code>SELECT customer_id, JSON_ARRAYAGG(JSON_OBJECT('amount', amount)) AS orders\nFROM orders\nGROUP BY customer_id;\n<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u6bcf\u500b\u5ba2\u6236\u7684\u6240\u6709\u8a02\u55ae\u91d1\u984d\u7684JSON\u6578\u7d44\uff0c\u6bcf\u500b\u91d1\u984d\u90fd\u88ab\u5305\u88dd\u5728\u4e00\u500bJSON\u5c0d\u8c61\u4e2d\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-sAQRDSewMp\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u4e2d\u7684<code>JSON_ARRAYAGG<\/code>\u548c<code>JSON_OBJECT<\/code>\u51fd\u6578\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8655\u7406\u548c\u751f\u6210JSON\u6578\u64da\u3002\u9019\u4e9b\u51fd\u6578\u4e0d\u50c5\u7c21\u5316\u4e86\u6578\u64da\u7684\u805a\u5408\u548c\u7d44\u5408\u904e\u7a0b\uff0c\u9084\u63d0\u9ad8\u4e86\u6578\u64da\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7528\u6027\u3002\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u638c\u63e1\u9019\u4e9b\u6280\u8853\u5c0d\u65bc\u958b\u767c\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u9700\u6c42\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u696d\u52d9\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u4e86\u89e3MySQL\u4e2d\u7684JSON_ARRAYAGG\u548cJSON_OBJECT\u51fd\u6570\uff0c\u638c\u63e1\u5982\u4f55\u9ad8\u6548\u5904\u7406\u548c\u751f\u6210JSON\u6570\u636e\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-180702","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180702","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=180702"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180702\/revisions"}],"predecessor-version":[{"id":180703,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180702\/revisions\/180703"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=180702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=180702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=180702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}