目录 MYSQL中的json数据操作 1.2 基础查询操作 1.2.1 一般json查询 1.2.2 多个条件查询 1.2.3 json中多个字段关系查询 1.2.4 关联表查询 1.3 JSON函数操作 1.3.1 官方json函数 1.3.2 -、-区别 1.3.2.2 在w
目录MYSQL中的json数据操作1.2 基础查询操作1.2.1 一般json查询1.2.2 多个条件查询1.2.3 json中多个字段关系查询1.2.4 关联表查询1.3 JSON函数操作1.3.1 官方json函数1.3.2 ->、->>区别1.3.2.2 在where条件中使用1.3.3 json_extract():从json中返回想要的字段1.3.4 JSON_CONTAINS():JSON格式数据是否在字段中包含特定对象1.3.5 SON_OBJECT():将一个键值对列表转换成json对象1.3.6 JSON_ARRAY():创建JSON数组1.3.7 JSON_TYPE():查询某个json字段属性类型1.3.8 JSON_KEYS():JSON文档中的键数组1.3.10 JSON_INSERT():插入值(往json中插入新值,但不替换已经存在的旧值)1.3.11 JSON_REPLACE()1.3.12 JSON_REMOVE():从JSON文档中删除数据
MYSQL中的json数据操作
1.2 基础查询操作
用法提示:
如果json字符串不是数组,则直接使用$.字段名如果json字符串是数组[Array],则直接使用$[对应元素的索引id]
1.2.1 一般json查询
使用json字段名->’$.json属性’进行查询条件,注意:如果 ‘->’ 不能用也可用 ‘->>’ 查询举个例子:如果想查询deptLeader=张五的数据,那么sql语句如下:
SELECT * from dept WHERE json_value->'$.deptLeaderId'='5';
1.2.2 多个条件查询
比如想查dept为“部门3”和deptLeaderId=5的数据,sql如下:
SELECT * from dept WHERE json_value->'$.deptLeaderId'='5' and json_value->'$.deptId'='5';
1.2.3 json中多个字段关系查询
比如想查询json格式中deptLeader=张五和deptId=5的数据
SELECT * from dept WHERE json_value->'$.deptLeaderId'='5' and json_value->'$.deptId'='5';
1.2.4 关联表查询
这里我们要连表查询在dept 表中部门leader在dept_leader 中的详情
SELECT * from dept,dept_leader
WHERE dept.json_value->'$.deptLeaderId'=dept_leader.json_value->'$.id' ;
1.3 JSON函数操作
写到这里大家都发现了,我们查询的json都是整条json数据,这样看起来不是很方便,那么如果我们只想看json中的某个字段怎么办?
1.3.1 官方json函数
NameDescription解释
-> |
Return value from JSON column after evaluating path; equivalent to JSON_EXTRACT() |
计算路径后返回JSON列的值;相当于JSON_EXTRACT () |
->> |
Return value from JSON column after evaluating path and unquoting the result; equivalent to JSON_UNQUOTE(JSON_EXTRACT()). |
从JSON列返回值后,就算路径和取消引号的结果;相当于JSON_UNQUOTE (JSON_EXTRACT ()) |
JSON_ARRAY() |
Create JSON array |
创建JSON数组 |
JSON_ARRAY_APPEND() |
Append data to JSON document |
向JSON文档追加数据 |
JSON_ARRAY_INSERT() |
Insert into JSON array |
插入JSON数组 |
JSON_CONTAINS() |
Whether JSON document contains specific object at path |
JSON文档是否包含路径上的特定对象 |
JSON_CONTAINS_PATH() |
Whether JSON document contains any data at path |
JSON文档是否在路径上包含任何数据 |
JSON_DEPTH() |
Maximum depth of JSON document |
JSON文档的最大深度 |
JSON_EXTRACT() |
Return data from JSON document |
从JSON文档返回数据 |
JSON_INSERT() |
Insert data into JSON document |
将数据插入JSON文档 |
JSON_KEYS() |
Array of keys from JSON document |
来自JSON文档的键数组 |
JSON_LENGTH() |
Number of elements in JSON document |
JSON文档中的元素数量 |
JSON_MERGE_PATCH() |
Merge JSON documents, replacing values of duplicate keys |
合并JSON文档,替换重复键的值 |
JSON_MERGE_PRESERVE() |
Merge JSON documents, preserving duplicate keys |
合并JSON文档,保留重复的密钥 |
JSON_OBJECT() |
Create JSON object |
创建JSON对象 |
JSON_OVERLAPS() |
Compares two JSON documents, returns TRUE (1) if these have any key-value pairs or array elements in common, otherwise FALSE (0) |
比较两个JSON文档,如果它们有共同的键值对或数组元素,则返回TRUE(1),否则返回FALSE (0) |
JSON_PRETTY() |
Print a JSON document in human-readable format |
以人类可读的格式打印JSON文档 |
JSON_QUOTE() |
Quote JSON document |
引用JSON文档 |
JSON_REMOVE() |
Remove data from JSON document |
从JSON文档中删除数据 |
JSON_REPLACE() |
Replace values in JSON document |
替换JSON文档中的值 |
JSON_SCHEMA_VALID() |
Validate JSON document against JSON schema; returns TRUE/1 if document validates against schema, or FALSE/0 if it does not |
针对JSON模式验证JSON文档;如果文档针对模式进行验证,则返回TRUE/1,否则返回FALSE/0 |
JSON_SCHEMA_VALIDATION_REPORT() |
Validate JSON document against JSON schema; returns report in JSON format on outcome on validation including success or failure and reasons for failure |
针对JSON模式验证JSON文档;以JSON格式返回关于验证结果的报告,包括成功或失败以及失败原因 |
JSON_SEARCH() |
Path to value within JSON document |
JSON文档中值的路径 |
JSON_SET() |
Insert data into JSON document |
将数据插入JSON文档 |
JSON_STORAGE_FREE() |
Freed space within binary representation of JSON column value following partial update |
在部分更新后释放JSON列值的二进制表示形式中的空间 |
JSON_STORAGE_SIZE() |
pace used for storage of binary representation of a JSON document |
用于存储JSON文档的二进制表示的空间 |
JSON_TABLE() |
Return data from a JSON expression as a relational table |
以关系表的形式从JSON表达式返回数据 |
JSON_TYPE() |
Type of JSON value |
JSON值类型 |
JSON_UNQUOTE() |
Unquote JSON value |
不引用JSON值 |
JSON_VALID() |
Whether JSON value is valid |
JSON值是否有效 |
JSON_VALUE() |
Extract value from JSON document at location pointed to by path provided; return this value as VARCHAR(512) or specified type |
根据所提供的路径从JSON文档中所指向的位置提取值;返回该值为VARCHAR(512)或指定的类型 |
MEMBER OF() |
Returns true (1) if first operand matches any element of JSON array passed as second operand, otherwise returns false (0) |
如果第一个操作数匹配作为第二个操作数的JSON数组中的任何元素,则返回true(1),否则返回false (0) |
1.3.2 ->、->>区别
->在field中使用的时候结果带引号,->>的结果不带引号