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