Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 【原創】MySQL5.7 JSON類型使用介紹

【原創】MySQL5.7 JSON類型使用介紹

編輯:關於android開發

【原創】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數據類型,但是我建議如果要使用的話,最好是把這樣的值取出來,然後在應用程序段來計算,畢竟數據庫是用來處理簡單數據的。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved