ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Mysql使用函数json\_extract处理Json类型数据 # 1. 需求概述 业务开发中通常mysql数据库中某个字段会需要存储json格式字符串,查询的时候有时json数据较大,每次全部取出再去解析查询效率较低,也比较麻烦,则Mysql5.7版本提供提供函数json\_extract,可以通过key查询value值,比较方便。 2\. json\_extract简介 2.1 函数简介 ****Mysql5.7****版本以后新增的功能,Mysql提供了一个原生的Json类型,Json值将不再以字符串的形式存储,而是采用一种允许快速读取文本元素(document elements)的内部二进制(internal binary)格式。 在Json列插入或者更新的时候将会自动验证Json文本,未通过验证的文本将产生一个错误信息。 Json文本采用标准的创建方式,可以使用大多数的比较操作符进行比较操作,例如:=, <, <=, >, >=, <>, != 和 <=>。 2.2 使用方式 数据存储的数据是json字符串(类型是vachar)。 想要查询出来json中某个字段的值,用到方法是:JSON\_EXTRACT()。 语法: JSON\_EXTRACT(json\_doc, path\[, path\] …) 实际用法: 如果json字符串不是数组,则直接使用$.字段名即可 ## 2.3 注意事项 ## JSON\_EXTRACT性能验证 , 通过查看执行计划,验证全部都是全表扫描。 ## 使用场景:数据量不大json字符串较大则可以采用,数据量较大不建议使用。 ``` <pre class="calibre13">``` <span class="token">--</span> 创建测试表 <span class="token5">CREATE</span> <span class="token5">TABLE</span> <span><span class="token4">`tab_json`</span></span> <span class="token1">(</span> <span><span class="token4">`id`</span></span> <span class="token3">bigint</span><span class="token1">(</span><span class="token5">20</span><span class="token1">)</span> <span class="token5">NOT</span> <span class="token5">NULL</span> <span class="token5">AUTO_INCREMENT</span> <span class="token5">COMMENT</span> <span class="token4">'主键id'</span><span class="token1">,</span> <span><span class="token4">`data`</span></span> json <span class="token5">DEFAULT</span> <span class="token5">NULL</span> <span class="token5">COMMENT</span> <span class="token4">'json字符串'</span><span class="token1">,</span> <span class="token5">PRIMARY</span> <span class="token5">KEY</span> <span class="token1">(</span><span><span class="token4">`id`</span></span><span class="token1">)</span> <span class="token1">)</span> <span class="token5">ENGINE</span><span class="token">=</span>InnoDB <span class="token5">DEFAULT</span> <span class="token5">CHARSET</span><span class="token">=</span>utf8<span class="token1">;</span> <span class="token">--</span> 新增数据 <span class="token">--</span> <span class="token1">{</span><span class="token4">"Tel"</span><span class="token1">:</span> <span class="token4">"132223232444"</span><span class="token1">,</span> <span class="token4">"name"</span><span class="token1">:</span> <span class="token4">"david"</span><span class="token1">,</span> <span class="token4">"address"</span><span class="token1">:</span> <span class="token4">"Beijing"</span><span class="token1">}</span> <span class="token">--</span> <span class="token1">{</span><span class="token4">"Tel"</span><span class="token1">:</span> <span class="token4">"13390989765"</span><span class="token1">,</span> <span class="token4">"name"</span><span class="token1">:</span> <span class="token4">"Mike"</span><span class="token1">,</span> <span class="token4">"address"</span><span class="token1">:</span> <span class="token4">"Guangzhou"</span><span class="token1">}</span> <span class="token5">INSERT</span> <span class="token5">INTO</span> <span><span class="token4">`testdb`</span></span><span class="token1">.</span><span><span class="token4">`tab_json`</span></span><span class="token1">(</span><span><span class="token4">`id`</span></span><span class="token1">,</span> <span><span class="token4">`data`</span></span><span class="token1">)</span> <span class="token5">VALUES</span> <span class="token1">(</span><span class="token5">1</span><span class="token1">,</span> <span class="token4">'{\"Tel\": \"132223232444\", \"name\": \"david\", \"address\": \"Beijing\"}'</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">INSERT</span> <span class="token5">INTO</span> <span><span class="token4">`testdb`</span></span><span class="token1">.</span><span><span class="token4">`tab_json`</span></span><span class="token1">(</span><span><span class="token4">`id`</span></span><span class="token1">,</span> <span><span class="token4">`data`</span></span><span class="token1">)</span> <span class="token5">VALUES</span> <span class="token1">(</span><span class="token5">2</span><span class="token1">,</span> <span class="token4">'{\"Tel\": \"13390989765\", \"name\": \"Mike\", \"address\": \"Guangzhou\"}'</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">INSERT</span> <span class="token5">INTO</span> <span><span class="token4">`testdb`</span></span><span class="token1">.</span><span><span class="token4">`tab_json`</span></span><span class="token1">(</span><span><span class="token4">`id`</span></span><span class="token1">,</span> <span><span class="token4">`data`</span></span><span class="token1">)</span> <span class="token5">VALUES</span> <span class="token1">(</span><span class="token5">3</span><span class="token1">,</span> <span class="token4">'{"success": true,"code": "0","message": "","data": {"name": "jerry","age": "18","sex": "男"}}'</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">INSERT</span> <span class="token5">INTO</span> <span><span class="token4">`testdb`</span></span><span class="token1">.</span><span><span class="token4">`tab_json`</span></span><span class="token1">(</span><span><span class="token4">`id`</span></span><span class="token1">,</span> <span><span class="token4">`data`</span></span><span class="token1">)</span> <span class="token5">VALUES</span> <span class="token1">(</span><span class="token5">4</span><span class="token1">,</span> <span class="token4">'{"success": true,"code": "1","message": "","data": {"name": "tome","age": "30","sex": "女"}}'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">--</span> 查询 select <span class="token">*</span> <span class="token2">from</span> tab_json<span class="token1">;</span> <span class="token">--</span> json_extract select <span class="token3">json_extract</span><span class="token1">(</span><span class="token4">'{"name":"Zhaim","tel":"13240133388"}'</span><span class="token1">,</span><span class="token4">"$.tel"</span><span class="token1">)</span><span class="token1">;</span> select <span class="token3">json_extract</span><span class="token1">(</span><span class="token4">'{"name":"Zhaim","tel":"13240133388"}'</span><span class="token1">,</span><span class="token4">"$.name"</span><span class="token1">)</span><span class="token1">;</span> <span class="token">--</span> 对tab_json表使用json_extract函数 select <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span> <span class="token2">from</span> tab_json<span class="token1">;</span> #如果查询没有的key<span class="token1">,</span>那么是可以查询<span class="token1">,</span>不过返回的是<span class="token5">NULL</span><span class="token1">.</span> select <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span><span class="token1">,</span><span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.Tel'</span><span class="token1">)</span> <span class="token2">from</span> tab_json<span class="token1">;</span> select <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span><span class="token1">,</span><span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.tel'</span><span class="token1">)</span> <span class="token2">from</span> tab_json<span class="token1">;</span> select <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span><span class="token1">,</span><span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.address'</span><span class="token1">)</span> <span class="token2">from</span> tab_json<span class="token1">;</span> <span class="token">--</span> 条件查询 select <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span><span class="token1">,</span><span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.Tel'</span><span class="token1">)</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span> <span class="token">=</span> <span class="token4">'Mike'</span><span class="token1">;</span> <span class="token">--</span> 嵌套json查询 select <span class="token">*</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.success'</span><span class="token1">)</span> <span class="token">=</span> <span class="token5">true</span><span class="token1">;</span> select <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.success'</span><span class="token1">)</span> <span class="token">=</span> <span class="token5">true</span><span class="token1">;</span> <span class="token">--</span> 查询data对应json中key为name的值 select <span class="token3">json_extract</span><span class="token1">(</span> <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.code'</span><span class="token1">)</span> <span class="token">=</span> <span class="token4">"1"</span><span class="token1">;</span> select <span class="token3">json_extract</span><span class="token1">(</span> <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span><span class="token1">,</span><span class="token3">json_extract</span><span class="token1">(</span> <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'$.age'</span><span class="token1">)</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.code'</span><span class="token1">)</span> <span class="token">=</span> <span class="token4">"0"</span><span class="token1">;</span> <span class="token">--</span> 性能验证 <span class="token1">,</span> 通过验证全部都是全表扫描,使用场景:数据量不大json字符串较大则可以采用,数据量较大不建议使用。 explain select <span class="token">*</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.success'</span><span class="token1">)</span> <span class="token">=</span> <span class="token5">true</span><span class="token1">;</span> explain select <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.success'</span><span class="token1">)</span> <span class="token">=</span> <span class="token5">true</span><span class="token1">;</span> <span class="token">--</span> 查询data对应json中key为name的值 explain select <span class="token3">json_extract</span><span class="token1">(</span> <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.code'</span><span class="token1">)</span> <span class="token">=</span> <span class="token4">"1"</span><span class="token1">;</span> explain select <span class="token3">json_extract</span><span class="token1">(</span> <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'$.name'</span><span class="token1">)</span><span class="token1">,</span><span class="token3">json_extract</span><span class="token1">(</span> <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.data'</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'$.age'</span><span class="token1">)</span> <span class="token2">from</span> tab_json where <span class="token3">json_extract</span><span class="token1">(</span>data<span class="token1">,</span><span class="token4">'$.code'</span><span class="token1">)</span> <span class="token">=</span> <span class="token4">"0"</span><span class="token1">;</span> ``` ``` ![](https://img.kancloud.cn/87/d1/87d18577caa7f530786197cb26d9e4c7_799x374.png)