{"id":7130,"date":"2024-11-14T14:19:43","date_gmt":"2024-11-14T06:19:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7130\/"},"modified":"2024-11-14T14:19:43","modified_gmt":"2024-11-14T06:19:43","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mysql-%e8%81%94%e5%90%88%e6%9f%a5%e8%af%a2%e8%8e%b7%e5%8f%96%e5%b5%8c%e5%a5%97-json-%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7130\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MySQL \u8054\u5408\u67e5\u8be2\u83b7\u53d6\u5d4c\u5957 JSON \u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173046618466213.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 MySQL \u8054\u5408\u67e5\u8be2\u83b7\u53d6\u5d4c\u5957 JSON \u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 MySQL \u8054\u5408\u67e5\u8be2\u83b7\u53d6\u5d4c\u5957 JSON \u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> <\/strong>\u4e2d\u7684<strong>\u8054\u5408\u67e5\u8be2\uff1a\u83b7\u53d6\u5d4c\u5957 json \u6570\u636e<\/strong><\/p>\n<p>\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93 mysql \u4e2d\uff0c\u4e3a\u4e86\u5904\u7406\u4e00\u5bf9\u591a\u5173\u7cfb\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u8054\u5408\u67e5\u8be2\u3002\u7ed9\u5b9a\u4e00\u5f20\u5b66\u751f\u8868 (student) \u548c\u4e00\u5f20\u56fe\u7247\u8868 (images)\uff0c\u5176\u4e2d\u4e00\u5f20\u5b66\u751f\u56fe\u7247\u4e0e\u591a\u4e2a\u56fe\u7247\u76f8\u5173\u8054\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u83b7\u53d6\u4ee5\u4e0b json \u683c\u5f0f\u7684\u67e5\u8be2\u7ed3\u679c\uff1a<\/p>\n<pre>{\n  \"id\": 1,\n  \"name\": \"john doe\",\n  \"images\": [\n    {\n      \"id\": 1,\n      \"url\": \"image1.jpg\"\n    },\n    {\n      \"id\": 2,\n      \"url\": \"image2.jpg\"\n    }\n  ]\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u63a5\u53e3\u5b9e\u73b0<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u6b64\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b sql \u67e5\u8be2\uff1a<\/p>\n<pre>select\n  s.id,\n  s.name,\n  group_concat(json_object(\"id\", i.id, \"url\", i.url)) as images\nfrom\n  student s\nleft join\n  images i on s.id = i.l_id\ngroup by\n  s.id, s.name<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u57fa\u4e8e\u5b66\u751f id \u5bf9\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u5c06\u56fe\u7247\u4fe1\u606f\u62fc\u63a5\u6210 json \u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u5728\u5206\u7ec4\u6210\u767e\u4e0a\u5343\u7684\u56fe\u50cf\u65f6\u51fa\u73b0\u6027\u80fd\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u57fa\u4e8e\u56fe\u50cf id \u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<pre>SELECT\n  s.id,\n  s.name,\n  JSON_ARRAYAGG(JSON_OBJECT(\"id\", i.id, \"url\", i.url)) AS images\nFROM\n  student s\nLEFT JOIN\n  images i ON s.id = i.l_id\nGROUP BY\n  s.id, s.name<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u4e3a\u6bcf\u5f20\u56fe\u50cf\u751f\u6210\u4e00\u4e2a json \u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528 json_arrayagg \u5c06\u5b83\u4eec\u805a\u5408\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 MySQL \u8054\u5408\u67e5\u8be2\u83b7\u53d6\u5d4c\u5957 JSON \u6570\u636e\uff1f\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>\u4e2d\u7684\u8054\u5408\u67e5\u8be2\uff1a\u83b7\u53d6\u5d4c\u5957 json \u6570\u636e \u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93 mysql \u4e2d\uff0c\u4e3a\u4e86\u5904\u7406\u4e00\u5bf9\u591a\u5173\u7cfb\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u8054\u5408\u67e5\u8be2\u3002\u7ed9\u5b9a\u4e00\u5f20\u5b66\u751f\u8868 (student) \u548c\u4e00\u5f20\u56fe\u7247\u8868 (images)\uff0c\u5176\u4e2d\u4e00\u5f20\u5b66\u751f\u56fe\u7247\u4e0e\u591a\u4e2a\u56fe\u7247\u76f8\u5173\u8054\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u83b7\u53d6\u4ee5\u4e0b json \u683c\u5f0f\u7684\u67e5\u8be2\u7ed3\u679c\uff1a { &#8220;id&#8221;: 1, &#8220;name&#8221;: &#8220;john doe&#8221;, &#8220;images&#8221;: [ { &#8220;id&#8221;: 1, &#8220;url&#8221;: &#8220;image1.jpg&#8221; }, { &#8220;id&#8221;: 2, &#8220;url&#8221;: &#8220;image2.jpg&#8221; } ] } \u767b\u5f55\u540e\u590d\u5236 \u63a5\u53e3\u5b9e\u73b0 \u8981\u5b9e\u73b0\u6b64\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b sql \u67e5\u8be2\uff1a select s.id, s.name, group_concat(json_object(&#8220;id&#8221;, i.id, &#8220;url&#8221;, i.url)) as images from student s left join images i on [&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-7130","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7130","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=7130"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7130\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}