mysql 处理 json 字段
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)
2.json_extract函数的使用
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)
3.对tab_json表使用json_extract函数
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)
本文作者:bitterteaer
本文链接:https://www.cnblogs.com/bitterteaer/p/17848588.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步