{"id":6178,"date":"2024-11-14T11:42:40","date_gmt":"2024-11-14T03:42:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6178\/"},"modified":"2024-11-14T11:42:40","modified_gmt":"2024-11-14T03:42:40","slug":"mysql%e5%a6%82%e4%bd%95%e5%ad%98%e5%82%a8%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6178\/","title":{"rendered":"mysql\u5982\u4f55\u5b58\u50a8\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u6570\u7ec4\uff0c\u4f46\u6709\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u53d8\u76f8\u5b58\u50a8\uff1ajson \u5217\uff1a\u4f7f\u7528 json \u683c\u5f0f\u5b58\u50a8\u6570\u7ec4\uff0c\u9002\u7528\u4e8e\u590d\u6742\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u3002\u5b57\u7b26\u4e32\u5e8f\u5217\u5316\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5b58\u50a8\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u6570\u7ec4\uff0c\u8bbf\u95ee\u6216\u4fee\u6539\u4e0d\u9891\u7e41\u3002\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff1a\u9002\u7528\u4e8e\u590d\u6742\u6570\u7ec4\uff0c\u9700\u8981\u9ad8\u7ea7\u4f18\u5316\u529f\u80fd\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061404332616639.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u5b58\u50a8\u6570\u7ec4\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u5b58\u50a8\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u5b58\u50a8\u6570\u7ec4<\/strong><\/p>\n<p>MySQL \u662f\u4e00\u79cd\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf (RDBMS)\uff0c\u5b83\u9ed8\u8ba4\u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u6570\u7ec4\u3002\u7136\u800c\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u53d8\u76f8\u5730\u5b58\u50a8\u6570\u7ec4\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1aJSON \u5217<\/strong><\/p>\n<p>JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u662f\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u683c\u5f0f\uff0c\u652f\u6301\u5b58\u50a8\u5d4c\u5957\u6570\u636e\u7ed3\u6784\uff0c\u5305\u62ec\u6570\u7ec4\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f7f\u7528 JSON \u5217\u5b58\u50a8\u6570\u7ec4\uff1a<\/p>\n<pre>CREATE TABLE my_table (\n    id INT PRIMARY KEY,\n    data JSON\n);\n\nINSERT INTO my_table (id, data) VALUES (1, JSON_ARRAY(1, 2, 3));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4f1a\u5728 my_table \u8868\u4e2d\u521b\u5efa\u4e00\u5217\u540d\u4e3a data\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 JSON \u6570\u7ec4\u3002<\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u5b57\u7b26\u4e32\u5e8f\u5217\u5316<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6570\u7ec4\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5c06\u6570\u7ec4\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u7528\u5206\u9694\u7b26\u8fde\u63a5\uff08\u4f8b\u5982\u9017\u53f7\uff09\u3002<\/li>\n<li>\u5c06\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u8868\u7684\u6587\u672c\u5217\u4e2d\u3002<\/li>\n<li>\u5728\u9700\u8981\u65f6\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u62c6\u5206\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u56de\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<pre>CREATE TABLE my_table (\n    id INT PRIMARY KEY,\n    data VARCHAR(255)\n);\n\nINSERT INTO my_table (id, data) VALUES (1, '1,2,3');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b (UDT) \u6765\u4e13\u95e8\u5b58\u50a8\u6570\u7ec4\u3002\u8fd9\u9700\u8981\u9ad8\u7ea7 MySQL \u77e5\u8bc6\uff0c\u5e76\u4e14\u4e0d\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u7ec4\u7684\u590d\u6742\u6027\u548c\u8bbf\u95ee\u6a21\u5f0f\u3002<\/p>\n<ul>\n<li> <strong>JSON \u5217\uff1a<\/strong>\u9002\u7528\u4e8e\u5b58\u50a8\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u7684\u590d\u6742\u6570\u7ec4\u3002<\/li>\n<li> <strong>\u5b57\u7b26\u4e32\u5e8f\u5217\u5316\uff1a<\/strong>\u9002\u7528\u4e8e\u6bd4\u8f83\u7b80\u5355\u7684\u6570\u7ec4\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u9891\u7e41\u7684\u8bbf\u95ee\u6216\u4fee\u6539\u3002<\/li>\n<li> <strong>\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff1a<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u7ea7\u529f\u80fd\u6216\u7279\u5b9a\u4f18\u5316\u573a\u666f\u7684\u590d\u6742\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5b58\u50a8\u6570\u7ec4\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 \u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u6570\u7ec4\uff0c\u4f46\u6709\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u53d8\u76f8\u5b58\u50a8\uff1ajson \u5217\uff1a\u4f7f\u7528 json \u683c\u5f0f\u5b58\u50a8\u6570\u7ec4\uff0c\u9002\u7528\u4e8e\u590d\u6742\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u3002\u5b57\u7b26\u4e32\u5e8f\u5217\u5316\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5b58\u50a8\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u6570\u7ec4\uff0c\u8bbf\u95ee\u6216\u4fee\u6539\u4e0d\u9891\u7e41\u3002\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff1a\u9002\u7528\u4e8e\u590d\u6742\u6570\u7ec4\uff0c\u9700\u8981\u9ad8\u7ea7\u4f18\u5316\u529f\u80fd\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u5b58\u50a8\u6570\u7ec4 MySQL \u662f\u4e00\u79cd\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf (RDBMS)\uff0c\u5b83\u9ed8\u8ba4\u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u6570\u7ec4\u3002\u7136\u800c\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u53d8\u76f8\u5730\u5b58\u50a8\u6570\u7ec4\u3002 \u65b9\u6cd5 1\uff1aJSON \u5217 JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u662f\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u683c\u5f0f\uff0c\u652f\u6301\u5b58\u50a8\u5d4c\u5957\u6570\u636e\u7ed3\u6784\uff0c\u5305\u62ec\u6570\u7ec4\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f7f\u7528 JSON \u5217\u5b58\u50a8\u6570\u7ec4\uff1a CREATE TABLE my_table ( id INT PRIMARY KEY, data JSON ); INSERT INTO my_table (id, data) VALUES (1, JSON_ARRAY(1, 2, 3)); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4f1a\u5728 my_table \u8868\u4e2d\u521b\u5efa\u4e00\u5217\u540d\u4e3a data\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 JSON \u6570\u7ec4\u3002 \u65b9\u6cd5 2\uff1a\u5b57\u7b26\u4e32\u5e8f\u5217\u5316 \u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6570\u7ec4\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u5c06\u6570\u7ec4\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u7528\u5206\u9694\u7b26\u8fde\u63a5\uff08\u4f8b\u5982\u9017\u53f7\uff09\u3002 \u5c06\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u8868\u7684\u6587\u672c\u5217\u4e2d\u3002 \u5728\u9700\u8981\u65f6\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u62c6\u5206\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u56de\u6570\u7ec4\u3002 CREATE TABLE my_table [&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-6178","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6178","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=6178"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6178\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}