json_extract(mysql5.7版本+)备案

282 阅读1分钟

-- 创建测试表 CREATE TABLE nice ( name text ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- 新增数据

INSERT INTO nice(name) VALUES ('{"Tel": "122", "name": "nice1", "address": "11111"}'); INSERT INTO nice( name) VALUES ('{"Tel": "1221211", "name": "nice2", "address": "111111"}');

-- 查询 select * from nice;

-- json_extract

select json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel");

-- 对tab_json表使用json_extract函数

select json_extract(data,'$.name') from tab_json;

#如果查询没有的key,那么是可以查询,不过返回的是NULL.

select json_extract(data,'$.name'),json_extract(data,'$.Tel') from nice;

-- 下面sql效果同上

select name->'$.name' from nice ; select name->'$.Area.Province' from nice ;

-- 性能验证 , 通过验证全部都是全表扫描,使用场景:数据量不大json字符串较大则可以采用,数据量较大不建议使用。

explain select * from nice where json_extract(name,'$.Tel') = "122";