{"id":953,"date":"2025-01-15T00:31:43","date_gmt":"2025-01-14T16:31:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=953"},"modified":"2025-01-15T00:31:43","modified_gmt":"2025-01-14T16:31:43","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-postgresql-%e4%b8%ad%e6%8f%90%e5%8f%96%e7%89%b9%e5%ae%9a%e5%88%97%e4%bd%9c%e4%b8%ba-json-%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%ef%bc%9a%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/15\/%e5%a6%82%e4%bd%95%e5%9c%a8-postgresql-%e4%b8%ad%e6%8f%90%e5%8f%96%e7%89%b9%e5%ae%9a%e5%88%97%e4%bd%9c%e4%b8%ba-json-%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%ef%bc%9a%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/","title":{"rendered":"\u5982\u4f55\u5728 PostgreSQL \u4e2d\u63d0\u53d6\u7279\u5b9a\u5217\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4\uff1a\u8be6\u7ec6\u6559\u7a0b\u4e0e\u793a\u4f8b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c\u63d0\u53d6\u7279\u5b9a\u5217\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u5728\u6d89\u53ca\u5230\u6570\u636e\u8f6c\u5316\u3001API \u8fd4\u56de\u683c\u5f0f\u3001\u4ee5\u53ca\u590d\u6742\u67e5\u8be2\u65f6\u3002PostgreSQL \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684 JSON \u51fd\u6570\u548c\u64cd\u4f5c\u7b26\uff0c\u4f7f\u5f97\u8fd9\u79cd\u4efb\u52a1\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u7075\u6d3b\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8868\u4e2d\u7684\u5217\u6570\u636e\u8f6c\u6362\u6210 JSON \u683c\u5f0f\uff0c\u751a\u81f3\u662f JSON \u6570\u7ec4\uff0c\u9002\u5e94\u4e0d\u540c\u7684\u4e1a\u52a1\u9700\u6c42\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u57fa\u672c\u6982\u5ff5\uff1aPostgreSQL \u4e2d\u7684 JSON \u548c JSONB \u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c\u6709\u4e24\u79cd\u4e3b\u8981\u7684 JSON \u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JSON<\/strong>\uff1a\u7528\u4e8e\u5b58\u50a8 JSON \u683c\u5f0f\u7684\u6587\u672c\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>JSONB<\/strong>\uff1a\u4e0e JSON \u7c7b\u578b\u7c7b\u4f3c\uff0c\u4f46\u5177\u6709\u4e8c\u8fdb\u5236\u683c\u5f0f\u5b58\u50a8\uff0c\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u67e5\u8be2\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u7d22\u5f15\u65b9\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e24\u79cd\u7c7b\u578b\u90fd\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u7ed3\u6784\u5316\u7684\u6570\u636e\uff0c\u4e14\u652f\u6301\u5f3a\u5927\u7684 JSON \u64cd\u4f5c\u548c\u67e5\u8be2\u529f\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5982\u4f55\u63d0\u53d6\u7279\u5b9a\u5217\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>employees<\/code> \u7684\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>id<\/code>\uff1a\u5458\u5de5\u7684\u552f\u4e00\u6807\u8bc6\u7b26<\/li>\n\n\n\n<li><code>name<\/code>\uff1a\u5458\u5de5\u7684\u59d3\u540d<\/li>\n\n\n\n<li><code>position<\/code>\uff1a\u5458\u5de5\u7684\u804c\u4f4d<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5e0c\u671b\u63d0\u53d6 <code>id<\/code> \u548c <code>name<\/code> \u4e24\u4e2a\u5b57\u6bb5\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 <code>jsonb_agg<\/code> \u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JSON \u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c<code>jsonb_agg()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u67e5\u8be2\u7ed3\u679c\u805a\u5408\u4e3a JSON \u6570\u7ec4\u3002\u7ed3\u5408 <code>json_build_object()<\/code> \u6216 <code>jsonb_build_object()<\/code> \u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7279\u5b9a\u7684\u5217\u8f6c\u6362\u4e3a JSON \u5bf9\u8c61\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u805a\u5408\u4e3a JSON \u6570\u7ec4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 1\uff1a\u63d0\u53d6\u7279\u5b9a\u5217\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT jsonb_agg(jsonb_build_object('id', id, 'name', name))\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u67e5\u8be2\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>jsonb_build_object('id', id, 'name', name)<\/code>\uff1a\u5c06 <code>id<\/code> \u548c <code>name<\/code> \u5b57\u6bb5\u5408\u5e76\u4e3a\u4e00\u4e2a JSON \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>jsonb_agg()<\/code>\uff1a\u805a\u5408\u6240\u6709\u8fd9\u4e9b JSON \u5bf9\u8c61\u6210\u4e00\u4e2a JSON \u6570\u7ec4\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe <code>employees<\/code> \u8868\u4e2d\u6709\u5982\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>position<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>John<\/td><td>Developer<\/td><\/tr><tr><td>2<\/td><td>Alice<\/td><td>Designer<\/td><\/tr><tr><td>3<\/td><td>Bob<\/td><td>Manager<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u4e0a\u8ff0\u67e5\u8be2\u540e\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a JSON \u6570\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n  {\"id\": 1, \"name\": \"John\"},\n  {\"id\": 2, \"name\": \"Alice\"},\n  {\"id\": 3, \"name\": \"Bob\"}\n]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b 2\uff1a\u63d0\u53d6\u6240\u6709\u5217\u5e76\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u63d0\u53d6\u6574\u4e2a\u8868\u683c\uff08\u6240\u6709\u5217\uff09\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>row_to_json()<\/code> \u51fd\u6570\u3002\u6b64\u51fd\u6570\u5c06\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a JSON \u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT jsonb_agg(row_to_json(employees))\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5217\u7684 JSON \u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u8868\u793a\u4e00\u4e2a\u5458\u5de5\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n  {\"id\": 1, \"name\": \"John\", \"position\": \"Developer\"},\n  {\"id\": 2, \"name\": \"Alice\", \"position\": \"Designer\"},\n  {\"id\": 3, \"name\": \"Bob\", \"position\": \"Manager\"}\n]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 <code>json_agg<\/code> \u548c <code>json_build_object<\/code> \u51fd\u6570\u7684\u5bf9\u6bd4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u63d0\u4f9b\u4e86 <code>json_agg()<\/code> \u548c <code>jsonb_agg()<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210 JSON \u548c JSONB \u683c\u5f0f\u7684\u805a\u5408\u7ed3\u679c\u3002\u4e00\u822c\u6765\u8bf4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>json_agg()<\/code><\/strong>\uff1a\u751f\u6210\u6807\u51c6\u7684 JSON \u683c\u5f0f\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><strong><code>jsonb_agg()<\/code><\/strong>\uff1a\u751f\u6210 JSONB \u683c\u5f0f\u7684\u6570\u7ec4\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66f4\u9ad8\u6548\u7684\u67e5\u8be2\u548c\u7d22\u5f15\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6ca1\u6709\u7279\u5b9a\u9700\u6c42\u7684\u60c5\u51b5\u4e0b\uff0c<code>jsonb_agg()<\/code> \u548c <code>json_agg()<\/code> \u4e4b\u95f4\u7684\u9009\u62e9\u4e3b\u8981\u53d6\u51b3\u4e8e\u4f60\u5bf9\u6027\u80fd\u7684\u9700\u6c42\u3002\u5982\u679c\u9700\u8981\u66f4\u9ad8\u6548\u7684\u5b58\u50a8\u548c\u67e5\u8be2\uff0c\u63a8\u8350\u4f7f\u7528 <code>jsonb<\/code> \u7c7b\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5728\u5b50\u67e5\u8be2\u4e2d\u63d0\u53d6 JSON \u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u8fd8\u53ef\u4ee5\u5728\u5b50\u67e5\u8be2\u4e2d\u4f7f\u7528\u8fd9\u4e9b JSON \u51fd\u6570\uff0c\u4ece\u800c\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u7684\u67e5\u8be2\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u53ea\u60f3\u63d0\u53d6\u4e00\u4e2a\u7279\u5b9a\u804c\u4f4d\u7684\u5458\u5de5\u4fe1\u606f\u5e76\u8f6c\u6362\u4e3a JSON \u6570\u7ec4\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT position, jsonb_agg(jsonb_build_object('id', id, 'name', name))\nFROM employees\nWHERE position = 'Developer'\nGROUP BY position;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u8fd4\u56de\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n  {\"id\": 1, \"name\": \"John\"}\n]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u590d\u6742 JSON \u8f6c\u6362<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u8fdb\u884c\u66f4\u590d\u6742\u7684 JSON \u8f6c\u6362\uff0c\u4f8b\u5982\u5d4c\u5957 JSON \u5bf9\u8c61\u3001\u6761\u4ef6\u8f6c\u6362\u7b49\uff0cPostgreSQL \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684 JSON \u51fd\u6570\uff0c\u5141\u8bb8\u4f60\u8f7b\u677e\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5c06\u6bcf\u4e2a\u5458\u5de5\u7684\u4fe1\u606f\u4f5c\u4e3a\u4e00\u4e2a\u5d4c\u5957\u7684 JSON \u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u62ec\u5458\u5de5\u7684 <code>id<\/code>\u3001<code>name<\/code> \u548c\u4e00\u4e2a <code>details<\/code> \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT jsonb_agg(jsonb_build_object(\n  'id', id,\n  'name', name,\n  'details', jsonb_build_object('position', position, 'department', 'IT')\n))\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u8fd4\u56de\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n  {\"id\": 1, \"name\": \"John\", \"details\": {\"position\": \"Developer\", \"department\": \"IT\"}},\n  {\"id\": 2, \"name\": \"Alice\", \"details\": {\"position\": \"Designer\", \"department\": \"IT\"}},\n  {\"id\": 3, \"name\": \"Bob\", \"details\": {\"position\": \"Manager\", \"department\": \"IT\"}}\n]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684 JSON \u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>jsonb_agg()<\/code> \u548c <code>json_build_object()<\/code> \u7b49\u51fd\u6570\uff0c\u5c06\u7279\u5b9a\u5217\u63d0\u53d6\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4\u3002\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3a JSON \u6570\u7ec4\u6216\u5bf9\u8c61\uff0c\u65b9\u4fbf API \u8fd4\u56de\u683c\u5f0f\u3002<\/li>\n\n\n\n<li>\u6839\u636e\u6761\u4ef6\u63d0\u53d6\u7279\u5b9a\u6570\u636e\uff0c\u5e76\u5d4c\u5957\u6210\u66f4\u590d\u6742\u7684 JSON \u7ed3\u6784\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 JSONB \u7c7b\u578b\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\uff0c\u5c24\u5176\u5728\u8fdb\u884c\u5927\u91cf\u6570\u636e\u5904\u7406\u65f6\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5408\u7406\u4f7f\u7528 PostgreSQL \u7684 JSON \u529f\u80fd\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u548c\u5b58\u50a8\uff0c\u9002\u5e94\u591a\u79cd\u4e1a\u52a1\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0c\u63d0\u53d6\u7279\u5b9a\u5217\u4f5c\u4e3a JSON \u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u5728\u6d89\u53ca\u5230\u6570\u636e\u8f6c\u5316\u3001API [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-953","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/953\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/953\/revisions\/954"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}