JSON是一種輕量級的數(shù)據(jù)交換格式,采用了獨立于語言的文本格式,類似XML,但是比XML簡單,易讀并且易編寫。對機器來說易于解析和生成,并且會減少網(wǎng)絡(luò)帶寬的傳輸。
JSON的格式非常簡單:名稱/鍵值。之前MySQL版本里面要實現(xiàn)這樣的存儲,要么用VARCHAR要么用TEXT大文本。 MySQL5.7發(fā)布后,專門設(shè)計了JSON數(shù)據(jù)類型以及關(guān)于這種類型的檢索以及其他函數(shù)解析。 我們先看看MySQL老版本的JSON存取。
示例表結(jié)構(gòu):
CREATE TABLE json_test(
id INT,
person_desc TEXT
)ENGINE INNODB;
我們來插入一條記錄:
NSERT INTO json_test VALUES (1,'{
"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格式的話,只能把這條記錄取出來交個應(yīng)用程序,有應(yīng)用程序來解析。
現(xiàn)在到了MySQL5.7,我們重新修改下表結(jié)構(gòu):
ALTER TABLE json_test MODIFY person_desc json;
先看看插入的這行JSON數(shù)據(jù)有哪些KEY:
mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_test\G
*************************** 1. row ***************************
id: 1
keys: ["authors", "musicians", "programmers"]
row in set (0.00 sec)
我們可以看到,里面有三個KEY,分別為authors,musicians,programmers。那現(xiàn)在找一個KEY把對應(yīng)的值拿出來:
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)
現(xiàn)在來把詳細的值羅列出來:
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"
row in set (0.00 sec)
我們進一步來演示把authors 這個KEY對應(yīng)的所有對象刪掉。
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
查找下對應(yīng)的KEY,發(fā)現(xiàn)已經(jīng)被刪除掉了。
mysql> SELECT json_contains_path(person_desc,'all','$.authors') as authors_exist
s FROM json_test\G
*************************** 1. row ***************************
authors_exists: 0
row in set (0.00 sec)
總結(jié)下, 雖然MySQL5.7 開始支持JSON數(shù)據(jù)類型,但是我建議如果要使用的話,最好是把這樣的值取出來,然后在應(yīng)用程序段來計算,畢竟數(shù)據(jù)庫是用來處理簡單數(shù)據(jù)的。
總結(jié)
以上所述是小編給大家介紹的MySQL5.7 JSON類型使用詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- MySQL5.7中的JSON基本操作指南
- Mysql5.7中JSON操作函數(shù)使用說明
- 簡單談?wù)凪ySQL5.7 JSON格式檢索
- mysql5.6及以下版本如何查詢數(shù)據(jù)庫里的json
- 詳解Mysql中的JSON系列操作函數(shù)
- MySQL操作之JSON數(shù)據(jù)類型操作詳解
- MySQL插入json問題
- mysql(5.6及以下)解析json的方法實例詳解
- mysql5.7 新增的json字段類型用法實例分析