mysql json_extract函數獲取json字段中某個key的值


json_extract函數可以獲取json對象中指定key的值,用法:json_extract(json_filed,"$.key")
舉例1:
mysql> select json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel");
+--------------------------------------------------------------+
| json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel") |
+--------------------------------------------------------------+
| "13240133388"                                                |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
舉例2:
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)
 
mysql> select json_extract(data,'$.name') from tab_json;
+-----------------------------+
| json_extract(data,'$.name') |
+-----------------------------+
| "david" |
| "Mike" |
+-----------------------------+
2 rows in set (0.00 sec)

如果查詢沒有的key,那么是可以查詢,不過返回的是NULL。

mysql> select json_extract(data,'$.name'),json_extract(data,'$.tel') from tab_json;
+-----------------------------+----------------------------+
| json_extract(data,'$.name') | json_extract(data,'$.tel') |
+-----------------------------+----------------------------+
| "david" | NULL |
| "Mike" | NULL |
+-----------------------------+----------------------------+
2 rows in set (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM