日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql的extract_mysql中json_extract函数的使用?作用是什么?

發(fā)布時間:2024/4/14 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql的extract_mysql中json_extract函数的使用?作用是什么? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

需求描述:

今天看mysql中的json數(shù)據(jù)類型,涉及到一些使用,使用到了函數(shù)json_extract來

獲取json字段中某個key的值,在此記錄下.

操作過程:

1.查看包含json字段的表信息

mysql> desc tab_json;

+-------+------------+------+-----+---------+----------------+

| Field | Type?????? | Null | Key | Default | Extra????????? |

+-------+------------+------+-----+---------+----------------+

| id??? | bigint(20) | NO?? | PRI | NULL??? | auto_increment |

| data? | json?????? | YES? |???? | NULL??? |??????????????? |

+-------+------------+------+-----+---------+----------------+

2 rows in set (0.00 sec)

mysql> show create table tab_json;

+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table??? | Create Table??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? |

+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| tab_json | CREATE TABLE `tab_json` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,

`data` json DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |

+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> select * from tab_json;

+----+----------------------------------------------------------------+

| id | data?????????????????????????????????????????????????????????? |

+----+----------------------------------------------------------------+

|? 1 | {"Tel": "132223232444", "name": "david", "address": "Beijing"} |

|? 2 | {"Tel": "13390989765", "name": "Mike", "address": "Guangzhou"} |

+----+----------------------------------------------------------------+

2 rows in set (0.00 sec)

備注:data字段就是json的數(shù)據(jù)類型,由鍵值對組成.

2.json_extract函數(shù)的使用

mysql> select json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel");

+--------------------------------------------------------------+

| json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel") |

+--------------------------------------------------------------+

| "13240133388" |

+--------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> select json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.name");

+---------------------------------------------------------------+

| json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.name") |

+---------------------------------------------------------------+

| "Zhaim" |

+---------------------------------------------------------------+

1 row in set (0.00 sec)

備注:這個查詢時直接對json對象進行操作.

3.對tab_json表使用json_extract函數(shù)

mysql> select json_extract(data,'$.name') from tab_json;

+-----------------------------+

| json_extract(data,'$.name') |

+-----------------------------+

| "david" |

| "Mike" |

+-----------------------------+

2 rows in set (0.00 sec)

mysql> select json_extract(data,'$.name'),json_extract(data,'$.tel') from tab_json; #如果查詢沒有的key,那么是可以查詢,不過返回的是NULL.

+-----------------------------+----------------------------+

| json_extract(data,'$.name') | json_extract(data,'$.tel') |

+-----------------------------+----------------------------+

| "david" | NULL |

| "Mike" | NULL |

+-----------------------------+----------------------------+

2 rows in set (0.00 sec)

mysql> select json_extract(data,'$.name'),json_extract(data,'$.address') from tab_json;

+-----------------------------+--------------------------------+

| json_extract(data,'$.name') | json_extract(data,'$.address') |

+-----------------------------+--------------------------------+

| "david" | "Beijing" |

| "Mike" | "Guangzhou" |

+-----------------------------+--------------------------------+

2 rows in set (0.00 sec)

備注:通過json_extract函數(shù),獲取到了json對象的特定key的值.

文檔創(chuàng)建時間:2018年6月5日14:25:41

總結(jié)

以上是生活随笔為你收集整理的mysql的extract_mysql中json_extract函数的使用?作用是什么?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。