MySQL的json查询之插入、合并

MySQL的json查询之插入、合并

MySQL的json查询之json_insert、json_merge_patch、json_merge_preserve、josn_remove、json_replace、json_set
MySQL的json查询之插入、合并
1. json_insert就是向json中插入,如果不存在则插入,存在则忽略
2. json_replace就是替换json中的项,如果不存在则忽略,存在则替换
3. json_set结合前面俩个,存在则替换,不存在则插入
4. json_merge_patch多个json进行合并,相同键名,后面的覆盖前面的,如果值是对象,则递归进行处理
5. json_merge_preserve多个json进行合并,相同键名,则键值组成新的对象
6. json_remove移除掉json某一项
数据表
MySQL的json查询之插入、合并
json_insert
例一
select json_insert(info, '$.age', 26) from member;

MySQL的json查询之插入、合并

json中并不存在age键名,则插入
例二

select json_insert(info, '$.name', 'swk') from member;

MySQL的json查询之插入、合并

json中存在name键名,则忽略
json_replace
例一

select json_replace(info, '$.name', 'swk') from member;

MySQL的json查询之插入、合并

json中存在name键名,则进行替换
例二

select json_replace(info, '$.age', 26) from member;

MySQL的json查询之插入、合并

json中不存在age键名,则忽略
json_set
例一

select json_set(info, '$.name', 'swk') from member;

MySQL的json查询之插入、合并

json中存在name键名,则进行替换
例二

select json_set(info, '$.age', 26) from member;

json中不存在age键名,则插入
json_merge_patch
例一

select json_merge_patch(info, '{"name":"swk","age":26}') from member;

MySQL的json查询之插入、合并

json合并,如果存在相同键名,则后面的覆盖前面的,如果值是对象,会递归
json_merge_preserve

select json_merge_preserve(info, '{"name":"swk","age":26}') from member;

MySQL的json查询之插入、合并

json合并,如果存在相同键名,则组成新的对象
json_remove
例一

select json_remove(info, '$.name') from member;

MySQL的json查询之插入、合并

移除json中指定项

Intoep小程序

微信扫一扫,打开小程序浏览更便捷

转载作品,原作者:梦中程序员,文章来源:https://www.toutiao.com/i7043625040687956484

发表回复

登录后才能评论