[mysql] JSON数据操作

/ 37,035评论 / 148706阅读 / 0点赞

前言

在MySql中使用

声明json类型的字段

create table test(
    uids json not null default ('[]')
);
default ('[{"name":"coolight"}, {"name","洛天依"}]')
或
default ("[{'name':'coolight'}, {'name','洛天依'}]")

Mysql中常用的操作json函数

JSON_LENGTH(json1)=JSON_LENGTH(json2) and JSON_CONTAINS(json1, json2)
// 例如:
update test set enable=true where JSON_LENGTH(json1)=JSON_LENGTH(json2) and JSON_CONTAINS(json1, json2);
// 示例:在数组末尾追加元素
SELECT JSON_ARRAY_APPEND('[1, 2, 3]', '$', 4); 
// 得到 [1,2,3,4]

// 示例:对数组添加一个元素,如果已经存在则不添加
// ["1000000", "1000001", "1000002"]
update test set uids=json_array_append(uids, '$','1000001') where id=777 and JSON_CONTAINS(uids,JSON_ARRAY('1000001'))=0;
// 没有添加,注意where ... and 后面 使用JSON_CONTAINS判断了是否包含准备添加的元素
// ["1000000", "1000001", "1000002"]
update test set uids=json_remove(uids, JSON_UNQUOTE(JSON_SEARCH(uids, 'one','1000001'))) where id=777;
// ["1000000", "1000002"]
create TRIGGER auto_reset after insert on test for each row
BEGIN
    update test set uids=json_array_append(uids, '$', CAST(NEW.uid as char)) where id=NEW.id;
END;
  1. Elmernem说道:

    how to pay with bitcoin on dark web tor drugs

  2. RichardSpela说道:

    current darknet markets reddit french deep web link

  3. AndrewHak说道:

    how to access the dark web through tor dark web sites links

  4. Zacharymom说道:

    best darknet market reddit 2023 gray market place

  5. JeromeVab说道:

    how to anonymously use darknet markets darknet market links reddit

  6. Willissog说道:

    how to enter the black market online incognito market darknet

  7. Johnnyfug说道:

    darknet market buying mdma usa tor2door link

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注