建立表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 (', , ],檢視插入的這行json資料有哪些key:"authors": [, , ],
"musicians": [, ]
}');
mysql> select id,json_keys(person_desc) as "keys" from json_test\g可以看到裡面有三個key,分別為authors,musicians,programmers。那現在找乙個key把對應的值拿出來:*************************** 1. row ***************************
id: 1
keys: ["authors", "musicians", "programmers"]
1 row in set (0.00 sec)
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:
*************************** 2. row ***************************
name: "peretti"
authors:
*************************** 3. row ***************************
name: "asimov"
authors:
3 rows in set (0.00 sec)
mysql> select刪掉authors這個key對應的所有物件:-> 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)
mysql> update json_test set person_desc = json_remove(person_desc,'$.authors')\g查詢對應的key,發現已經被刪除掉:query ok, 1 row affected (0.01 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> select json_contains_path(person_desc,'all','$.authors') as authors_exists from json_test\g
*************************** 1. row ***************************
authors_exists: 0
1 row in set (0.00 sec)
mysql5 7 新增的json欄位型別
一 我們先建立乙個表,準備點資料 create table json test id int 11 unsigned not null auto increment comment id json json default null comment json資料 二 檢索json列的字段 通過使用 ...
MySQL 5 7的新增功能
1 隨機 root 密碼 mysql 5.7 資料庫初始化完成後,會自動生成乙個 root localhost 使用者,root 使用者的密碼不為空,而是隨機產生乙個密碼。2 自定義 test 資料庫 mysql 5.7 預設安裝完成後沒有 test 資料庫。使用者可以自行建立 test 資料庫並對...
MySQL5 7中使用JSON(一)
因為專案需要,儲存字段儲存成了json格式,在專案中是將查詢出來的值通過jackson轉成相應的bean進行處理的,覺得不夠簡單方便。偶然下,知道了mysql5.7原生支援sql,今天一回來就折騰安裝了mysql5.7,所以才出現了上篇安裝綠色版mysql的博文,廢話不多說,研究了1個小時的mysq...