[mysql] JSON数据操作

/ 37,064评论 / 148868阅读 / 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. WesleyInags说道:

    list of dark net markets onion live

  2. WilliamGet说道:

    where to find darknet market links redit dark markets san marino

  3. Arnoldelide说道:

    which darknet market are still up buy drugs darknet

  4. AndrewRes说道:

    most popular darknet markets 2023 deep web hitmen url

  5. BillyDig说道:

    reddit darknet markets links dark market links

  6. Zacharymom说道:

    darknet market links 2023 reddit uk darknet markets

  7. EdwardTow说道:

    darknet market vendors search darknet market google

  8. AllenBus说道:

    how to access the black market dark web links

  9. Arnoldelide说道:

    what is the best darknet market archetyp market link

  10. DavidDiege说道:

    top darknet markets 2023 tor drugs

  11. JeromeVab说道:

    what darknet markets still work dark markets bosnia

  12. Zacharymom说道:

    counterfeit money dark web reddit darknet market francais

发表回复

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