{"id":6169,"date":"2024-11-14T14:24:26","date_gmt":"2024-11-14T06:24:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6169\/"},"modified":"2024-11-14T14:24:26","modified_gmt":"2024-11-14T06:24:26","slug":"mysql%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9json%e5%ad%97%e6%ae%b5%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6169\/","title":{"rendered":"mysql\u5982\u4f55\u4fee\u6539json\u5b57\u6bb5\u503c"},"content":{"rendered":"<blockquote><p>\n  mysql \u4fee\u6539 json \u5b57\u6bb5\u503c\u7684\u51e0\u79cd\u65b9\u6cd5\uff1ajson_set() \u51fd\u6570\uff1a\u8bbe\u7f6e\u6307\u5b9a\u8def\u5f84\u7684\u65b0\u503c\u3002json_replace() \u51fd\u6570\uff1a\u66ff\u6362\u6307\u5b9a\u8def\u5f84\u5904\u7684\u73b0\u6709\u503c\u3002json_modify() \u51fd\u6570\uff1a\u4f7f\u7528 json patch \u8bed\u6cd5\u7075\u6d3b\u4fee\u6539 json \u5b57\u6bb5\u3002json_remove() \u51fd\u6570\uff1a\u5220\u9664\u6307\u5b9a\u8def\u5f84\u7684\u5185\u5bb9\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061404032621165.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u4fee\u6539json\u5b57\u6bb5\u503c\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u4fee\u6539json\u5b57\u6bb5\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4fee\u6539 MySQL \u4e2d JSON \u5b57\u6bb5\u7684\u503c<\/strong><\/p>\n<p>MySQL \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u4fee\u6539 JSON \u5b57\u6bb5\u7684\u503c\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 JSON_SET() \u51fd\u6570<\/strong><\/p>\n<p>JSON_SET() \u51fd\u6570\u5141\u8bb8\u60a8\u4e3a JSON \u5b57\u6bb5\u4e2d\u7684\u7279\u5b9a\u8def\u5f84\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>JSON_SET(json_column, path, new_value)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u5c06 data JSON \u5b57\u6bb5\u4e2d name \u5b57\u6bb5\u7684\u503c\u4fee\u6539\u4e3a &#8220;John Doe&#8221;\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>UPDATE table_name SET data = JSON_SET(data, '$.name', 'John Doe')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 JSON_REPLACE() \u51fd\u6570<\/strong><\/p>\n<p>JSON_REPLACE() \u51fd\u6570\u66ff\u6362 JSON \u5b57\u6bb5\u4e2d\u7279\u5b9a\u8def\u5f84\u5904\u7684\u73b0\u6709\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>JSON_REPLACE(json_column, path, new_value)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u5c06 data JSON \u5b57\u6bb5\u4e2d address.city \u5b57\u6bb5\u7684\u503c\u4fee\u6539\u4e3a &#8220;New York&#8221;\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>UPDATE table_name SET data = JSON_REPLACE(data, '$.address.city', 'New York')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 JSON_MODIFY() \u51fd\u6570<\/strong><\/p>\n<p>JSON_MODIFY() \u51fd\u6570\u5141\u8bb8\u60a8\u4f7f\u7528 JSON Patch \u8bed\u6cd5\u6765\u4fee\u6539 JSON \u5b57\u6bb5\u3002\u8fd9\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u6027\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>JSON_MODIFY(json_column, json_patch)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u5728 data JSON \u5b57\u6bb5\u4e2d\u5411 tags \u6570\u7ec4\u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20 &#8220;sports&#8221;, \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>UPDATE table_name SET data = JSON_MODIFY(data, '$.tags', JSON_ARRAY_APPEND(data.tags, 'sports'))<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 4\uff1a\u4f7f\u7528 JSON_REMOVE() \u51fd\u6570<\/strong><\/p>\n<p>JSON_REMOVE() \u51fd\u6570\u5141\u8bb8\u60a8\u4ece JSON \u5b57\u6bb5\u4e2d\u5220\u9664\u7279\u5b9a\u8def\u5f84\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>JSON_REMOVE(json_column, path)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u4ece data JSON \u5b57\u6bb5\u4e2d\u5220\u9664 details \u5b57\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>UPDATE table_name SET data = JSON_REMOVE(data, '$.details')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4fee\u6539json\u5b57\u6bb5\u503c\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>mysql \u4fee\u6539 json \u5b57\u6bb5\u503c\u7684\u51e0\u79cd\u65b9\u6cd5\uff1ajson_set() \u51fd\u6570\uff1a\u8bbe\u7f6e\u6307\u5b9a\u8def\u5f84\u7684\u65b0\u503c\u3002json_replace() \u51fd\u6570\uff1a\u66ff\u6362\u6307\u5b9a\u8def\u5f84\u5904\u7684\u73b0\u6709\u503c\u3002json_modify() \u51fd\u6570\uff1a\u4f7f\u7528 json patch \u8bed\u6cd5\u7075\u6d3b\u4fee\u6539 json \u5b57\u6bb5\u3002json_remove() \u51fd\u6570\uff1a\u5220\u9664\u6307\u5b9a\u8def\u5f84\u7684\u5185\u5bb9\u3002 \u5982\u4f55\u4fee\u6539 MySQL \u4e2d JSON \u5b57\u6bb5\u7684\u503c MySQL \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u4fee\u6539 JSON \u5b57\u6bb5\u7684\u503c\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 JSON_SET() \u51fd\u6570 JSON_SET() \u51fd\u6570\u5141\u8bb8\u60a8\u4e3a JSON \u5b57\u6bb5\u4e2d\u7684\u7279\u5b9a\u8def\u5f84\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a JSON_SET(json_column, path, new_value) \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u8981\u5c06 data JSON \u5b57\u6bb5\u4e2d name \u5b57\u6bb5\u7684\u503c\u4fee\u6539\u4e3a &#8220;John Doe&#8221;\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a UPDATE table_name SET data = JSON_SET(data, &#8216;$.name&#8217;, &#8216;John Doe&#8217;) \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 JSON_REPLACE() [&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-6169","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6169","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=6169"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6169\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}