时间:2023-10-29来源:系统城装机大师作者:佚名
下面用这个表来执行查询演示:
| 1 2 3 4 5 6 |
CREATE TABLE `users` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL COMMENT '姓名', `address` json NOT NULL COMMENT '住址', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
插入几条数据
| 1 2 3 4 5 6 7 |
INSERT INTO `users` VALUES (1, '张三', '{\"city\": \"石家庄市\", \"tags\": [\"家\", \"公司\"], \"district\": \"桥西区\", \"province\": \"河北省\"}');INSERT INTO `users` VALUES (2, '李四', '{\"city\": \"广州市\", \"tags\": [\"宿舍\"], \"district\": \"珠海区\", \"province\": \"广州省\"}');INSERT INTO `users` VALUES (3, '王五', '{\"city\": \"长春市\", \"district\": \"绿园区\", \"province\": \"吉林省\"}');INSERT INTO `users` VALUES (4, '刘六', '{\"city\": \"昌平区\", \"province\": \"北京市\"}');INSERT INTO `users` VALUES (5, '张三三', '[{\"city\": \"石家庄市\", \"tags\": [\"家\", \"公司\", \"学校\"], \"district\": \"桥西区\", \"province\": \"河北省\"}, {\"city\": \"郑州市\", \"tags\": [\"宿舍\"], \"district\": \"桥东区\", \"province\": \"河南省\"}]');INSERT INTO `users` VALUES (6, '李四四', '[{\"city\": \"广州市\", \"tags\": [\"宿舍\"], \"district\": \"珠海区\", \"province\": \"广州省\"}, {\"city\": \"广州市\", \"district\": \"珠海区\", \"province\": \"广州省\"}]');INSERT INTO `users` VALUES (7, '王五六', '[\"家\", \"公司\", \"学校\"]'); |
1、函数查询:json_extract(json字段, '$.json属性')
| 1 | select * from users where json_extract(address, '$.province') = "河北省"; |

2、对象操作方法进行查询:json字段->'$.json属性'
| 1 | select * from users where address->'$.province' = "河北省"; |

1、数组操作方式查询:字段->'$[0]'
| 1 | select * from users where address->'$[0]'= "家"; |

1、函数查询:json_length(json字段)
| 1 | select * from users where json_length(address) = 2; |

1、函数查询:json_length(json字段)
| 1 | select * from users where json_length(address) = 2; |

1、函数查询:JSON_CONTAINS(json字段,JSON_OBJECT('json数组属性', '内容'))
| 1 | select * from users where JSON_CONTAINS(address,JSON_OBJECT('tags', '家')); |

| 1 | select * from users where address->'$[*].city' is not null; |

| 1 | select * from users where address->'$.tags' is not null; |

到此这篇关于关于Mysql中json数据类型的查询操作指南的文章就介绍到这了
2023-10-30
windows上的mysql服务突然消失提示10061 Unkonwn error问题及解决方案2023-10-30
MySQL非常重要的日志bin log详解2023-10-30
详解MySQL事务日志redo log一、单表查询 1、排序 2、聚合函数 3、分组 4、limit 二、SQL约束 1、主键约束 2、非空约束 3、唯一约束 4、外键约束 5、默认值 三、多表查询 1、内连接 1)隐式内连接: 2)显式内连接: 2、外连接 1)左外连接 2)右外连接 四...
2023-10-30
Mysql删除表重复数据 表里存在唯一主键 没有主键时删除重复数据 Mysql删除表中重复数据并保留一条 准备一张表 用的是mysql8 大家自行更改 创建表并添加四条相同的数据...
2023-10-30