MySQL中JSON数据的查找和更新
共计 1.1k words,预计阅读时间 6 min

设MySQL中JSON字段info的格式如下:

{
	"name":"小明",
	"age":15,
	"height":163
}

建表

create table 't1' (
	'id' int(11) NOT NULL AUTO_INCREMENT,
	'info' json DEFAULT NULL,
	PRIMARY KEY ('id')
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

查找

1. 查找JSON中的某个字段

select info->'$.name' from t1 where id = 1;
-- 或者
select JSON_EXTRACT(info,'$.name') from t1 where id = 1;

2. 根据JSON中的字段进行查找

select * from t1 where info->'$.name' = '小明';
-- 或者
select * from t1 where JSON_EXTRACT(info,'$.name') = '小明';

更新

1. 修改JSON中某个字段的值

JSON_REPLACE(JSON列名,JSON字段名,修改值) JSON_SET(JSON列名,JSON字段名,修改值)

update t1 set info = JSON_REPLACE(info,'$.age',16) where info->'$.name' = '小明';
-- 或者
update t1 set info = JSON_SET(info,'$.age',16) where id = 1;

2. 向JSON中添加一个字段

JSON_INSERT(JSON列名,添加的JSON字段名,值) JSON_SET(JSON列名,添加的JSON字段名,值)

update t1 set info = JSON_INSERT(info,'$.weight',34) where id = 1;
-- 或者
update t1 set info = JSON_SET(info,'$.weight',34) where id = 1;

即,JSON_SET函数既可以更新也可以添加,当所给字段存在时就是更新,当所给字段不存在时就是添加

JS字符串操作连用问题
SSH connect to host github.com port 22 Connection refused
copyright  2024   @ Cardy
Powered by Astro | Theme Cloud