[mysql] JSON数据操作

/ 20,352评论 / 83747阅读 / 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说道:

    cheap propecia without insurance: order propecia for sale – cost of cheap propecia without a prescription

  2. Spiopa说道:

    order famvir 250mg generic – cheap valaciclovir 500mg valaciclovir 500mg cost

  3. Gchzlm说道:

    digoxin usa – purchase lasix generic order furosemide 40mg pill

  4. Michaelsew说道:

    http://canadaph24.pro/# canadian pharmacy ed medications

  5. Marcelcrolf说道:

    http://mexicoph24.life/# pharmacies in mexico that ship to usa

  6. JustinTut说道:

    top online pharmacy india https://indiaph24.store/# online shopping pharmacy india
    world pharmacy india

  7. RickyAlage说道:

    top online pharmacy india Cheapest online pharmacy indian pharmacy paypal

  8. Marcelcrolf说道:

    https://canadaph24.pro/# canadian pharmacy near me

  9. RickyAlage说道:

    india pharmacy [url=http://indiaph24.store/#]buy medicines from India[/url] cheapest online pharmacy india

  10. Kvartiry_kfma说道:

    снять квартиру в Минске на сутки https://sutki24.by/ .

  11. Pedropaite说道:

    mexican online pharmacies prescription drugs: cheapest mexico drugs – п»їbest mexican online pharmacies

  12. Michaelsew说道:

    http://mexicoph24.life/# buying from online mexican pharmacy

  13. JustinTut说道:

    online pharmacy india https://indiaph24.store/# Online medicine order
    top 10 online pharmacy in india

  14. StevenSen说道:

    world pharmacy india: buy medicines from India – india pharmacy mail order

  15. Pedropaite说道:

    mexican drugstore online: Mexican Pharmacy Online – п»їbest mexican online pharmacies

  16. Marcelcrolf说道:

    http://indiaph24.store/# india pharmacy

  17. RickyAlage说道:

    indian pharmacy paypal [url=http://indiaph24.store/#]Generic Medicine India to USA[/url] indian pharmacies safe

  18. Michaelsew说道:

    https://mexicoph24.life/# reputable mexican pharmacies online

  19. Marcelcrolf说道:

    https://indiaph24.store/# india online pharmacy

  20. RickyAlage说道:

    mexico pharmacy [url=http://mexicoph24.life/#]mexico pharmacy[/url] pharmacies in mexico that ship to usa

  21. Marcelcrolf说道:

    http://indiaph24.store/# best india pharmacy

  22. RickyAlage说道:

    mexico pharmacy [url=https://mexicoph24.life/#]mexico pharmacy[/url] mexican pharmacy

  23. StevenSen说道:

    buying prescription drugs in mexico: mexico pharmacy – medication from mexico pharmacy

  24. Marcelcrolf说道:

    http://canadaph24.pro/# online canadian pharmacy

  25. RickyAlage说道:

    cheapest online pharmacy india [url=http://indiaph24.store/#]indian pharmacy fast delivery[/url] india online pharmacy

  26. Michaelsew说道:

    http://indiaph24.store/# buy medicines online in india

  27. Marcelcrolf说道:

    http://indiaph24.store/# top 10 pharmacies in india

  28. RickyAlage说道:

    mexico drug stores pharmacies [url=http://mexicoph24.life/#]Online Pharmacies in Mexico[/url] mexico pharmacies prescription drugs

  29. Marcelcrolf说道:

    https://mexicoph24.life/# mexico drug stores pharmacies

发表回复

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