[mysql] JSON数据操作

/ 20,442评论 / 88404阅读 / 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. DavidTyday说道:

    best darknet markets for vendors darknet market dmt

  2. IgnacioMob说道:

    best darknet market reddit 2023 dark web weed

  3. Charliebut说道:

    how to anonymously use darknet markets bitcoin dark website

  4. Ernestges说道:

    dark markets montenegro dark web cvv

  5. totosite说道:

    I’ve been searching for hours on this topic and finally found your post. totosite, I have read your post and I am very impressed. We prefer your opinion and will visit this site frequently to refer to your opinion. When would you like to visit my site?

  6. slotsite说道:

    I came to this site with the introduction of a friend around me and I was very impressed when I found your writing. I’ll come back often after bookmarking! slotsite

  7. uttenly说道:

    If you are being treated with any of the following medications, you should not drink green tea or take green tea extract without first talking to your health care provider cialis online pharmacy Contraindicated 1 prochlorperazine and pentamidine both increase QTc interval

发表回复

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