[mysql] JSON数据操作

/ 20,445评论 / 88438阅读 / 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. RandallGuesE说道:

    buy misoprostol over the counter: buy cytotec over the counter – buy cytotec

  2. WilliamRen说道:

    https://lisinopril.network/# buy lisinopril online uk

  3. WilliamRen说道:

    https://finasteride.store/# cost generic propecia for sale

  4. Davidfrify说道:

    Misoprostol 200 mg buy online buy cytotec over the counter buy cytotec pills

  5. RandallGuesE说道:

    buy ciprofloxacin: purchase cipro – buy cipro

  6. RonaldSwobe说道:

    https://nolvadex.life/# tamoxifen dosage

  7. Davidfrify说道:

    order cheap propecia no prescription [url=https://finasteride.store/#]cost generic propecia without insurance[/url] get propecia

  8. RonaldSwobe说道:

    https://cytotec.club/# cytotec online

  9. RandallGuesE说道:

    nolvadex price: tamoxifen and osteoporosis – tamoxifen for breast cancer prevention

  10. WilliamRen说道:

    http://nolvadex.life/# tamoxifen for gynecomastia reviews

  11. Davidfrify说道:

    tamoxifen nolvadex 20mg tamoxifen and ovarian cancer

  12. Gbooue说道:

    lopressor ca – olmesartan 20mg for sale buy adalat

  13. leading to a discussion about the impact of ラブドール sexthese standards on individual self-esteem and body image.

  14. RandallGuesE说道:

    tamoxifen and grapefruit: tamoxifen joint pain – nolvadex gynecomastia

  15. However, as I hit the world of college, オナドールI was a thousand miles plus an ocean away from my mother’s watchful eye and protection.

  16. CharlesMag说道:

    http://ciprofloxacin.tech/# buy cipro cheap
    tamoxifen blood clots [url=https://nolvadex.life/#]tamoxifen hip pain[/url] tamoxifen cancer

  17. RandallGuesE说道:

    where can i buy cipro online: ciprofloxacin order online – buy ciprofloxacin over the counter

  18. WilliamRen说道:

    http://cytotec.club/# cytotec online

  19. WilliamRen说道:

    http://nolvadex.life/# tamoxifen alternatives

  20. Davidfrify说道:

    cytotec buy online usa cytotec pills buy online order cytotec online

  21. Free sex说道:

    He is good at eating pickles and telling women about his emotional problems.

发表回复

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