佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > MySQL中json字段的操作方法

MySQL中json字段的操作方法

熱門標(biāo)簽:呼叫中心市場需求 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) Linux服務(wù)器 服務(wù)外包 百度競價(jià)排名 AI電銷 地方門戶網(wǎng)站

   MySQL5.7.8中引入了json字段,這種類型的字段使用的頻率比較低,但是在實(shí)際操作中,有些業(yè)務(wù)仍然在用,我們以此為例,介紹下json字段的操作方法:

還是從例子看起:

mysql> create table test1(id int,info json);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test1 values (1,'{"name":"yeyz","age":26}'),(2,'{"name":"zhangsan","age":30}'),(3,'{"name":"lisi","age":35}');
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from test1;
+------+---------------------------------+
| id  | info              |
+------+---------------------------------+
|  1 | {"age": 26, "name": "yeyz"}   |
|  2 | {"age": 30, "name": "zhangsan"} |
|  3 | {"age": 35, "name": "lisi"}   |
+------+---------------------------------+
3 rows in set (0.00 sec)

    首先我們創(chuàng)建了一個(gè)表test1,其中id是int字段,info是json字段,插入了三條數(shù)據(jù),如上:

mysql> select * from test1 where json_extract(info,"$.age")>=30;
+------+---------------------------------+
| id  | info              |
+------+---------------------------------+
|  2 | {"age": 30, "name": "zhangsan"} |
|  3 | {"age": 35, "name": "lisi"}   |
+------+---------------------------------+
2 rows in set (0.00 sec)

   我們可以通過json_extract的方法得到j(luò)son中的內(nèi)容。其中:

1、$符號(hào)代表的是json的根目錄,

2、我們使用$.age相當(dāng)于取出來了json中的age字段,

3、當(dāng)然,在函數(shù)最前面,應(yīng)該寫上字段名字info

下面來看json中常用的函數(shù):

a、json_valid判斷是否是json字段,如果是,返回1,如果不是,返回0

mysql> select json_valid(2);
+---------------+
| json_valid(2) |
+---------------+
|       0 |
+---------------+
1 row in set (0.01 sec)
mysql> select json_valid('{"num":2}');
+-------------------------+
| json_valid('{"num":2}') |
+-------------------------+
|            1 |
+-------------------------+
1 row in set (0.00 sec)

mysql> select json_valid('2');
+-----------------+
| json_valid('2') |
+-----------------+
|        1 |
+-----------------+
1 row in set (0.00 sec)
mysql> select json_valid('name');
+--------------------+
| json_valid('name') |
+--------------------+
|         0 |
+--------------------+
1 row in set (0.00 sec)

   這里需要注意的是,如果傳入了字符串2,那么,返回結(jié)果是1

b、json_keys傳回執(zhí)行json字段最上一層的key值

mysql> select json_keys('{"name":"yeyz","score":100}');
+------------------------------------------+
| json_keys('{"name":"yeyz","score":100}') |
+------------------------------------------+
| ["name", "score"]            |
+------------------------------------------+
1 row in set (0.01 sec)
mysql> select json_keys('{"name":"yeyz","score":{"math":100,"English":95}}');
+----------------------------------------------------------------+
| json_keys('{"name":"yeyz","score":{"math":100,"English":95}}') |
+----------------------------------------------------------------+
| ["name", "score"]                       |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
#如果有多層,可以在最后面使用$的方法,拿到其中的某一層的目錄
mysql> select json_keys('{"name":"yeyz","score":{"math":100,"English":95}}','$.score');
+--------------------------------------------------------------------------+
| json_keys('{"name":"yeyz","score":{"math":100,"English":95}}','$.score') |
+--------------------------------------------------------------------------+
| ["math", "English"]                           |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

c、json_length函數(shù),返回最上一層的key個(gè)數(shù),如果想取到中間的某一層,則可以使用$的方法,如下:

mysql> select json_length('{"name":"yeyz","score":{"math":100,"English":95},"age":26}');
+---------------------------------------------------------------------------+
| json_length('{"name":"yeyz","score":{"math":100,"English":95},"age":26}') |
+---------------------------------------------------------------------------+
|                                     3 |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select json_length('{"name":"yeyz","score":{"math":100,"English":95},"age":26}','$.score');
+-------------------------------------------------------------------------------------+
| json_length('{"name":"yeyz","score":{"math":100,"English":95},"age":26}','$.score') |
+-------------------------------------------------------------------------------------+
|                                          2 |
+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

d、json_depth函數(shù),json文件的深度,測試?yán)尤缦拢?/p>

mysql> select json_depth('{"aaa":1}'),json_depth('{}');
+-------------------------+------------------+
| json_depth('{"aaa":1}') | json_depth('{}') |
+-------------------------+------------------+
|            2 |        1 |
+-------------------------+------------------+
1 row in set (0.00 sec)

mysql> select json_depth('{"name":"yeyz","score":{"math":100,"English":95},"age":26}');
+--------------------------------------------------------------------------+
| json_depth('{"name":"yeyz","score":{"math":100,"English":95},"age":26}') |
+--------------------------------------------------------------------------+
|                                    3 |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

   這里需要注意的是,形如{'aa':1}這種形式的json,其深度是2

e、json_contains_path函數(shù)檢索json中是否有一個(gè)或者多個(gè)成員。

mysql> set @j='{"a":1,"b":2,"c":{"d":4}}';
Query OK, 0 rows affected (0.00 sec)
#one的意思是只要包含一個(gè)成員,就返回1
mysql> select json_contains_path(@j,'one','$.a','$.e');
+------------------------------------------+
| json_contains_path(@j,'one','$.a','$.e') |
+------------------------------------------+
|                    1 |
+------------------------------------------+
1 row in set (0.00 sec)
#all的意思是所有的成員都包含,才返回1
mysql> select json_contains_path(@j,'all','$.a','$.e');
+------------------------------------------+
| json_contains_path(@j,'all','$.a','$.e') |
+------------------------------------------+
|                    0 |
+------------------------------------------+
1 row in set (0.01 sec)

mysql> select json_contains_path(@j,'one','$.c.d');
+--------------------------------------+
| json_contains_path(@j,'one','$.c.d') |
+--------------------------------------+
|                  1 |
+--------------------------------------+
1 row in set (0.00 sec)

mysql> select json_contains_path(@j,'one','$.a.d');
+--------------------------------------+
| json_contains_path(@j,'one','$.a.d') |
+--------------------------------------+
|                  0 |
+--------------------------------------+
1 row in set (0.00 sec)

f、json_type函數(shù),判斷json中的成員的類型,需要和json_extract結(jié)合起來使用。

mysql> select * from test1;
+------+---------------------------------+
| id  | info              |
+------+---------------------------------+
|  1 | {"age": 26, "name": "yeyz"}   |
|  2 | {"age": 30, "name": "zhangsan"} |
|  3 | {"age": 35, "name": "lisi"}   |
+------+---------------------------------+
3 rows in set (0.00 sec)
#判斷name的類型
mysql> select json_type(json_extract(info,"$.name")) from test1;
+----------------------------------------+
| json_type(json_extract(info,"$.name")) |
+----------------------------------------+
| STRING                 |
| STRING                 |
| STRING                 |
+----------------------------------------+
3 rows in set (0.00 sec)
#判斷age的類型
mysql> select json_type(json_extract(info,"$.age")) from test1;
+---------------------------------------+
| json_type(json_extract(info,"$.age")) |
+---------------------------------------+
| INTEGER                |
| INTEGER                |
| INTEGER                |
+---------------------------------------+
3 rows in set (0.00 sec)
#判斷name和age組合起來的類型,可以看到是array
mysql> select json_type(json_extract(info,"$.name","$.age")) from test1;
+------------------------------------------------+
| json_type(json_extract(info,"$.name","$.age")) |
+------------------------------------------------+
| ARRAY                     |
| ARRAY                     |
| ARRAY                     |
+------------------------------------------------+
3 rows in set (0.00 sec)

g、*的作用,所有的值,看下面的例子。

{
 "a":1,
 "b":2,
 "c":
   {
    "d":4
   }
 "e":
   {
   "d":
     {
     "ddd":
     "5"
     }
   }
}
mysql> set @j='{"a":1,"b":2,"c":{"d":4},"e":{"d":{"ddd":"5"}}}';
Query OK, 0 rows affected (0.00 sec)
#所有成員
mysql> select json_extract(@j,'$.*');
+---------------------------------------+
| json_extract(@j,'$.*')        |
+---------------------------------------+
| [1, 2, {"d": 4}, {"d": {"ddd": "5"}}] |
+---------------------------------------+
1 row in set (0.00 sec)
#所有成員中的d成員
mysql> select json_extract(@j,'$.*.d');
+--------------------------+
| json_extract(@j,'$.*.d') |
+--------------------------+
| [4, {"ddd": "5"}]    |
+--------------------------+
1 row in set (0.00 sec)

以上就是MySQL中json字段的操作方法的詳細(xì)內(nèi)容,更多關(guān)于MySQL json字段的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 深入淺出講解MySQL的并行復(fù)制
  • MySQL刪除了記錄不生效的原因排查
  • MySQL CHAR和VARCHAR存儲(chǔ)、讀取時(shí)的差別
  • MySQL學(xué)習(xí)教程之聚簇索引
  • 淺談MySQL大表優(yōu)化方案
  • MySQL8.0中的降序索引
  • 詳解mysql中的存儲(chǔ)引擎
  • 記一次MySQL的優(yōu)化案例
  • mysql 主從復(fù)制如何跳過報(bào)錯(cuò)
  • 淺析MySQL并行復(fù)制

標(biāo)簽:黃山 衡水 湖南 崇左 湘潭 銅川 仙桃 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL中json字段的操作方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    长白| 仲巴县| 金阳县| 莱西市| 泸溪县| 龙山县| 元阳县| 宾川县| 湟源县| 盐津县| 邻水| 巴东县| 手机| 嘉荫县| 嘉义县| 绥滨县| 高州市| 鹿泉市| 浦东新区| 昂仁县| 通辽市| 金门县| 荥经县| 曲麻莱县| 鲜城| 安新县| 上虞市| 应城市| 旬阳县| 苍梧县| 临朐县| 汝阳县| 忻城县| 托克逊县| 大同县| 白城市| 吴忠市| 石狮市| 金川县| 天祝| 广德县|