mysql json格式的一些操作

 json表的创建

-- 创建表
CREATE TABLE mytest(id INT , sname VARCHAR(20) , jsontest JSON);

json的插入 

insert into mytest(id,name,jsontest) values(1,"name1",JSON_ARRAY("1","2",NULL,4));

insert into mytest(id,name,jsontest) values(1,"name1",JSON_OBJECT("name","tom","age",12));

insert into mytest(id,name,jsontest) values(1,"name1",'{"name":"tom","age":12}');

json的查询 

-- 查询
select name,JSON_EXTRACT(jsontest, '$.age') from mytest
-- 带条件查询
select name,jsontest->'$.age' from mytest where jsontest->'$.age' > 10

-- 查询keys
select name,JSON_KEYS(jsontest) from mytest

json修改 

update mytest set jsontest = JSON_SET(jsontest, '$.age', 99)

猜你喜欢

转载自blog.csdn.net/dmw412724/article/details/113128227