這篇文章帶給大家的內容是關於MySQL5.7的JSON基本操作(程式碼範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
MySQL5.7的JSON基本操作
MySQL從5.7版開始就支援JSON格式的數據,操作就挺方便的。
建表
在新表時欄位類型可以直接設定為json類型,例如我們建立一張表:
mysql> CREATE TABLE `test_user`(`id` INT PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(50) NOT NULL, `info` JSON);
json類型欄位可以為NULL
插入資料:
mysql> INSERT INTO test_user(`name`, `info`) VALUES('xiaoming','{"sex": 1, "age": 18, "nick_name": "小萌"}');
json類型的欄位必須時有效的json字串
#可以使用JSON_OBJECT()函數建構json物件:
mysql> INSERT INTO test_user(`name`, `info`) VALUES('xiaohua', JSON_OBJECT("sex", 0, "age", 17));
使用JSON_ARRAY()函數建構json數組:
mysql> INSERT INTO test_user(`name`, `info`) VALUES('xiaozhang', JSON_OBJECT("sex", 1, "age", 19, "tag", JSON_ARRAY(3,5,90)));
現在查看test_user表中的資料:
mysql> select * from test_user; +----+-----------+--------------------------------------------+ | id | name | info | +----+-----------+--------------------------------------------+ | 1 | xiaoming | {"age": 18, "sex": 1, "nick_name": "小萌"} | | 2 | xiaohua | {"age": 17, "sex": 0} | | 3 | xiaozhang | {"age": 19, "sex": 1, "tag": [3, 5, 90]} | +----+-----------+--------------------------------------------+ 3 rows in set (0.04 sec)
查詢
表達式: 物件為json列->'$.鍵', 陣列為json列->'$.鍵[index]'
mysql> select name, info->'$.nick_name', info->'$.sex', info->'$.tag[0]' from test_user; +-----------+---------------------+---------------+------------------+ | name | info->'$.nick_name' | info->'$.sex' | info->'$.tag[0]' | +-----------+---------------------+---------------+------------------+ | xiaoming | "小萌" | 1 | NULL | | xiaohua | NULL | 0 | NULL | | xiaozhang | NULL | 1 | 3 | +-----------+---------------------+---------------+------------------+ 3 rows in set (0.04 sec)
等價於:物件為JSON_EXTRACT(json列, '$.鍵'),陣列為JSON_EXTRACT(json列, ' $.鍵[index]')
mysql> select name, JSON_EXTRACT(info, '$.nick_name'), JSON_EXTRACT(info, '$.sex'), JSON_EXTRACT(info, '$.tag[0]') from test_user; +-----------+-----------------------------------+-----------------------------+--------------------------------+ | name | JSON_EXTRACT(info, '$.nick_name') | JSON_EXTRACT(info, '$.sex') | JSON_EXTRACT(info, '$.tag[0]') | +-----------+-----------------------------------+-----------------------------+--------------------------------+ | xiaoming | "小萌" | 1 | NULL | | xiaohua | NULL | 0 | NULL | | xiaozhang | NULL | 1 | 3 | +-----------+-----------------------------------+-----------------------------+--------------------------------+ 3 rows in set (0.04 sec)
不過看到上面"小萌"是帶雙引號的,這不是我們想要的,可以用JSON_UNQUOTE函數將雙引號去掉
#mysql> select name, JSON_UNQUOTE(info->'$.nick_name') from test_user where name='xiaoming'; +----------+-----------------------------------+ | name | JSON_UNQUOTE(info->'$.nick_name') | +----------+-----------------------------------+ | xiaoming | 小萌 | +----------+-----------------------------------+ 1 row in set (0.05 sec)
也可以直接使用運算符->>
mysql> select name, info->>'$.nick_name' from test_user where name='xiaoming'; +----------+----------------------+ | name | info->>'$.nick_name' | +----------+----------------------+ | xiaoming | 小萌 | +----------+----------------------+ 1 row in set (0.06 sec)
當然屬性也可以作為查詢條件
mysql> select name, info->>'$.nick_name' from test_user where info->'$.nick_name'='小萌'; +----------+----------------------+ | name | info->>'$.nick_name' | +----------+----------------------+ | xiaoming | 小萌 | +----------+----------------------+ 1 row in set (0.05 sec)
值得一提的是,可以透過虛擬列對JSON類型的指定屬性進行快速查詢。
建立虛擬列:
mysql> ALTER TABLE `test_user` ADD `nick_name` VARCHAR(50) GENERATED ALWAYS AS (info->>'$.nick_name') VIRTUAL;
注意用運算元->>
使用時和一般類型的列查詢是一樣:
mysql> select name,nick_name from test_user where nick_name='小萌'; +----------+-----------+ | name | nick_name | +----------+-----------+ | xiaoming | 小萌 | +----------+-----------+ 1 row in set (0.05 sec)
更新
使用JSON_INSERT()插入新值,但不會覆寫已經存在的值
mysql> UPDATE test_user SET info = JSON_INSERT(info, '$.sex', 1, '$.nick_name', '小花') where id=2;
看下結果
#mysql> select * from test_user where id=2; +----+---------+--------------------------------------------+-----------+ | id | name | info | nick_name | +----+---------+--------------------------------------------+-----------+ | 2 | xiaohua | {"age": 17, "sex": 0, "nick_name": "小花"} | 小花 | +----+---------+--------------------------------------------+-----------+ 1 row in set (0.06 sec)
使用JSON_SET()插入新值,並覆寫已經存在的值
mysql> UPDATE test_user SET info = JSON_INSERT(info, '$.sex', 0, '$.nick_name', '小张') where id=3;
看下結果
mysql> select * from test_user where id=3; +----+-----------+---------------------------------------------------------------+-----------+ | id | name | info | nick_name | +----+-----------+---------------------------------------------------------------+-----------+ | 3 | xiaozhang | {"age": 19, "sex": 1, "tag": [3, 5, 90], "nick_name": "小张"} | 小张 | +----+-----------+---------------------------------------------------------------+-----------+ 1 row in set (0.06 sec)
使用JSON_REPLACE()只取代存在的值
mysql> UPDATE test_user SET info = JSON_REPLACE(info, '$.sex', 1, '$.tag', '[1,2,3]') where id=2;
看下結果
mysql> select * from test_user where id=2; +----+---------+--------------------------------------------+-----------+ | id | name | info | nick_name | +----+---------+--------------------------------------------+-----------+ | 2 | xiaohua | {"age": 17, "sex": 1, "nick_name": "小花"} | 小花 | +----+---------+--------------------------------------------+-----------+ 1 row in set (0.06 sec)
可以看到tag沒有更新進去
刪除
使用JSON_REMOVE()刪除JSON元素
mysql> UPDATE test_user SET info = JSON_REMOVE(info, '$.sex', '$.tag') where id=1;
看下結果
mysql> select * from test_user where id=1; +----+----------+----------------------------------+-----------+ | id | name | info | nick_name | +----+----------+----------------------------------+-----------+ | 1 | xiaoming | {"age": 18, "nick_name": "小萌"} | 小萌 | +----+----------+----------------------------------+-----------+ 1 row in set (0.05 sec)
本篇文章到這裡就已經全部結束了,更多其他精彩內容可以關注PHP中文網的MySQL教學影片欄位!
#以上是MySQL5.7的JSON基本操作(程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!