WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

MySQL5.7中新增的JSON类型的使用方法

Posted on   WebEnh  阅读(285)  评论(0编辑  收藏  举报

创建表json_test:

CREATE TABLE json_test(id INT(11) AUTO_INCREMENT PRIMARY KEY,person_desc JSON)ENGINE INNODB;

插入一条记录:

INSERT INTO json_test(person_desc) VALUES ('{  
    "programmers": [{  
        "firstName": "Brett",  
        "lastName": "McLaughlin",  
        "email": "aaaa"  
    }, {  
        "firstName": "Jason",  
        "lastName": "Hunter",  
        "email": "bbbb"  
    }, {  
        "firstName": "Elliotte",  
        "lastName": "Harold",  
        "email": "cccc"  
    }],  
    "authors": [{  
        "firstName": "Isaac",  
        "lastName": "Asimov",  
        "genre": "sciencefiction"  
    }, {  
        "firstName": "Tad",  
        "lastName": "Williams",  
        "genre": "fantasy"  
    }, {  
        "firstName": "Frank",  
        "lastName": "Peretti",  
        "genre": "christianfiction"  
    }],  
    "musicians": [{  
        "firstName": "Eric",  
        "lastName": "Clapton",  
        "instrument": "guitar"  
    }, {  
        "firstName": "Sergei",  
        "lastName": "Rachmaninoff",  
        "instrument": "piano"  
    }]  
}');

查看插入的这行JSON数据有哪些KEY:

mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_test\G  
*************************** 1. row ***************************  
  id: 1  
keys: ["authors", "musicians", "programmers"]  
1 row in set (0.00 sec)

可以看到里面有三个KEY,分别为authors,musicians,programmers。那现在找一个KEY把对应的值拿出来:

mysql> SELECT json_extract(AUTHORS,'$.lastName[0]') AS 'name', AUTHORS FROM  
    -> (  
    -> SELECT id,json_extract(person_desc,'$.authors[0][0]') AS "authors" FROM json_test  
    -> UNION ALL  
    -> SELECT id,json_extract(person_desc,'$.authors[1][0]') AS "authors" FROM json_test  
    -> UNION ALL  
    -> SELECT id,json_extract(person_desc,'$.authors[2][0]') AS "authors" FROM json_test  
    -> ) AS T1  
    -> ORDER BY NAME DESC\G  
*************************** 1. row ***************************  
   name: "Williams"  
AUTHORS: {"genre": "fantasy", "lastName": "Williams", "firstName": "Tad"}  
*************************** 2. row ***************************  
   name: "Peretti"  
AUTHORS: {"genre": "christianfiction", "lastName": "Peretti", "firstName": "Frank"}  
*************************** 3. row ***************************  
   name: "Asimov"  
AUTHORS: {"genre": "sciencefiction", "lastName": "Asimov", "firstName": "Isaac"}  
  
  
3 rows in set (0.00 sec)

列出详细值:

mysql> SELECT  
    -> json_extract(AUTHORS,'$.firstName[0]') AS "firstname",  
    -> json_extract(AUTHORS,'$.lastName[0]') AS "lastname",  
    -> json_extract(AUTHORS,'$.genre[0]') AS "genre"  
    -> FROM  
    -> (  
    -> SELECT id,json_extract(person_desc,'$.authors[0]') AS "authors" FROM json  
_test  
    -> ) AS T\G  
*************************** 1. row ***************************  
firstname: "Isaac"  
 lastname: "Asimov"  
    genre: "sciencefiction"  
1 row in set (0.00 sec)

删掉authors这个KEY对应的所有对象:

mysql> UPDATE json_test SET person_desc = json_remove(person_desc,'$.authors')\G  
Query OK, 1 row affected (0.01 sec)  
Rows matched: 1  Changed: 1  Warnings: 0

查找对应的KEY,发现已经被删除掉:

mysql> SELECT json_contains_path(person_desc,'all','$.authors') as authors_exist  
s FROM json_test\G  
*************************** 1. row ***************************  
authors_exists: 0  
1 row in set (0.00 sec)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多