欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

【原创】MySQL5.7 JSON类型使用介绍_PHP教程

程序员文章站 2022-06-14 14:52:05
...

【原创】MySQL5.7 JSON类型使用介绍

JSON是一种轻量级的数据交换格式,采用了独立于语言的文本格式,类似XML,但是比XML简单,易读并且易编写。对机器来说易于解析和生成,并且会减少网络带宽的传输。
JSON的格式非常简单:名称/键值。之前MySQL版本里面要实现这样的存储,要么用VARCHAR要么用TEXT大文本。 MySQL5.7发布后,专门设计了JSON数据类型以及关于这种类型的检索以及其他函数解析。 我们先看看MySQL老版本的JSON存取。


示例表结构:

  1. CREATE TABLE json_test(
  2. id INT,
  3. person_desc TEXT
  4. )ENGINE INNODB;




我们来插入一条记录:


  1. INSERT INTO json_test VALUES (1,'{
  2. "programmers": [{
  3. "firstName": "Brett",
  4. "lastName": "McLaughlin",
  5. "email": "aaaa"
  6. }, {
  7. "firstName": "Jason",
  8. "lastName": "Hunter",
  9. "email": "bbbb"
  10. }, {
  11. "firstName": "Elliotte",
  12. "lastName": "Harold",
  13. "email": "cccc"
  14. }],
  15. "authors": [{
  16. "firstName": "Isaac",
  17. "lastName": "Asimov",
  18. "genre": "sciencefiction"
  19. }, {
  20. "firstName": "Tad",
  21. "lastName": "Williams",
  22. "genre": "fantasy"
  23. }, {
  24. "firstName": "Frank",
  25. "lastName": "Peretti",
  26. "genre": "christianfiction"
  27. }],
  28. "musicians": [{
  29. "firstName": "Eric",
  30. "lastName": "Clapton",
  31. "instrument": "guitar"
  32. }, {
  33. "firstName": "Sergei",
  34. "lastName": "Rachmaninoff",
  35. "instrument": "piano"
  36. }]
  37. }');



那一般我们遇到这样来存储JSON格式的话,只能把这条记录取出来交个应用程序,有应用程序来解析。


现在到了MySQL5.7,我们重新修改下表结构:



  1. ALTER TABLE json_test MODIFY person_desc json;


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

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




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

  1. mysql> SELECT json_extract(AUTHORS,'$.lastName[0]') AS 'name', AUTHORS FROM
  2. -> (
  3. -> SELECT id,json_extract(person_desc,'$.authors[0][0]') AS "authors" FROM json_test
  4. -> UNION ALL
  5. -> SELECT id,json_extract(person_desc,'$.authors[1][0]') AS "authors" FROM json_test
  6. -> UNION ALL
  7. -> SELECT id,json_extract(person_desc,'$.authors[2][0]') AS "authors" FROM json_test
  8. -> ) AS T1
  9. -> ORDER BY NAME DESC\G
  10. *************************** 1. row ***************************
  11. name: "Williams"
  12. AUTHORS: {"genre": "fantasy", "lastName": "Williams", "firstName": "Tad"}
  13. *************************** 2. row ***************************
  14. name: "Peretti"
  15. AUTHORS: {"genre": "christianfiction", "lastName": "Peretti", "firstName": "Frank"}
  16. *************************** 3. row ***************************
  17. name: "Asimov"
  18. AUTHORS: {"genre": "sciencefiction", "lastName": "Asimov", "firstName": "Isaac"}


  19. 3 rows in set (0.00 sec)


现在来把详细的值罗列出来:

  1. mysql> SELECT
  2. -> json_extract(AUTHORS,'$.firstName[0]') AS "firstname",
  3. -> json_extract(AUTHORS,'$.lastName[0]') AS "lastname",
  4. -> json_extract(AUTHORS,'$.genre[0]') AS "genre"
  5. -> FROM
  6. -> (
  7. -> SELECT id,json_extract(person_desc,'$.authors[0]') AS "authors" FROM json
  8. _test
  9. -> ) AS T\G
  10. *************************** 1. row ***************************
  11. firstname: "Isaac"
  12. lastname: "Asimov"
  13. genre: "sciencefiction"
  14. 1 row in set (0.00 sec)


我们进一步来演示把authors 这个KEY对应的所有对象删掉。


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


查找下对应的KEY,发现已经被删除掉了。

  1. mysql> SELECT json_contains_path(person_desc,'all','$.authors') as authors_exist
  2. s FROM json_test\G
  3. *************************** 1. row ***************************
  4. authors_exists: 0
  5. 1 row in set (0.00 sec)


总结下, 虽然MySQL5.7 开始支持JSON数据类型,但是我建议如果要使用的话,最好是把这样的值取出来,然后在应用程序段来计算,毕竟数据库是用来处理简单数据的。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1069967.htmlTechArticle【原创】MySQL5.7 JSON类型使用介绍 JSON是一种轻量级的数据交换格式,采用了独立于语言的文本格式,类似XML,但是比XML简单,易读并且易编...
相关标签: android