MySQL5.7 Search JSON Values 之 JSON_CONTAINS 方法简单使用
在一些业务场景下,我们会使用到MySQL
的json
类型字段,并且通过某些条件去匹配json
字段里的值返回查询结果。下面我们进入主题:
查看官方文档
做开发这一行,遇到问题,首先要查找官方文档,并且要有官方文档的好习惯。
进入官方文档,我们在侧边栏搜索找到JSON Functions相关文档,然后选择Functions That Search JSON Values,文档里介绍的第一个方法就是JSON_CONTAINS(target, candidate[, path])
。
文档描述
通过翻译文档,我们可以知道:
JSON_CONTAINS(target, candidate[, path])
通过返回0
或1
来表示目标JSON
中是否含有候选JSON
的内容,或根据给定的路径自变量里是否匹配到了候选JSON
对象。如果任何参数为NULL
,或者path
参数不能匹配到目标JSON
的字段,则返回NULL
。如果目标JSON
或候选JSON
对象不是有效的JSON
格式,或者path
参数不是有效的路径表达式或包含*
或**
通配符,则会发生错误。
Indicates by returning 1 or 0 whether a given candidate JSON document is contained within a target JSON document, or—if a path argument was supplied—whether the candidate is found at a specific path within the target. Returns NULL if any argument is NULL, or if the path argument does not identify a section of the target document. An error occurs if target or candidate is not a valid JSON document, or if the path argument is not a valid path expression or contains a * or ** wildcard.
读到这里我明白了
JSON_CONTAINS(target, candidate[, path])
方法里的target
、candidate
两个字段入参为JSON
对象,path
是可选值。
- 当且仅当候选标量可比较且相等时,才包含在目标标量中。如果两个标量值具有相同的
JSON_TYPE()
类型,则它们是可比较的,但类型INTEGER和DECIMAL的值也可以彼此比较。
A candidate scalar is contained in a target scalar if and only if they are comparable and are equal. Two scalar values are comparable if they have the same JSON_TYPE() types, with the exception that values of types INTEGER and DECIMAL are also comparable to each other.
- 当且仅当候选对象中的每个元素都包含在目标的某个元素中时,候选数组才包含在目标数组中。
A candidate array is contained in a target array if and only if every element in the candidate is contained in some element of the target.
- 当且仅当候选非数组包含在目标的某个元素中时,该候选非数组才包含在目标数组中。
A candidate nonarray is contained in a target array if and only if the candidate is contained in some element of the target.
- 当且仅当候选对象中的每个关键字在目标中存在一个具有相同名称的关键字并且与候选关键字相关联的值包含在与目标关键字相关联的值中时,候选对象才包含在目标对象中。
A candidate object is contained in a target object if and only if for each key in the candidate there is a key with the same name in the target and the value associated with the candidate key is contained in the value associated with the target key.
实践
以上理论我们看完了,对JSON_CONTAINS(target, candidate[, path])
有了一定了解,现在我们开始实践,才能彻底理解、掌握JSON_CONTAINS(target, candidate[, path])
方法的使用:
这里需要补充说明下
path
的格式是以$
开头,若目为数组则用$[number/*]
定位数组下标或匹配,若目标为对象则使用$.name
定位字段。
- 当且仅当候选标量可比较且相等时,才包含在目标标量中。如果两个标量值具有相同的JSON_TYPE()类型,则它们是可比较的,但类型INTEGER和DECIMAL的值也可以彼此比较。
这里我们验证一下类型INTEGER和DECIMAL的值比较
SELECT JSON_CONTAINS('{"name": "book", "price": 3.00, "number": 100}', '3', '$.price');
+---------------------------------------------------------------------------------+
| JSON_CONTAINS('{"name": "book", "price": 3.00, "number": 100}', '3', '$.price') |
+---------------------------------------------------------------------------------+
| 1 |
+---------------------------------------------------------------------------------+
- 当且仅当候选对象中的每个元素都包含在目标的某个元素中时,候选数组才包含在目标数组中。
SELECT JSON_CONTAINS(' ["1", "2", "3", "4", "5"]', '["1", "2"]');
+-----------------------------------------------------------+
| JSON_CONTAINS(' ["1", "2", "3", "4", "5"]', '["1", "2"]') |
+-----------------------------------------------------------+
| 1 |
+-----------------------------------------------------------+
SELECT JSON_CONTAINS('{"numbers": ["1", "2", "3", "4", "5"]}', '["1", "2"]', '$.numbers');
+------------------------------------------------------------------------------------+
| JSON_CONTAINS('{"numbers": ["1", "2", "3", "4", "5"]}', '["1", "2"]', '$.numbers') |
+------------------------------------------------------------------------------------+
| 1 |
+------------------------------------------------------------------------------------+
SELECT JSON_CONTAINS('{"numbers": ["1", "2", "3", "4", "5"]}', '["6", "5"]', '$.numbers');
+------------------------------------------------------------------------------------+
| JSON_CONTAINS('{"numbers": ["1", "2", "3", "4", "5"]}', '["6", "5"]', '$.numbers') |
+------------------------------------------------------------------------------------+
| 0 |
+------------------------------------------------------------------------------------+
- 当且仅当候选非数组包含在目标的某个元素中时,该候选非数组才包含在目标数组中。
- 当且仅当候选对象中的每个关键字在目标中存在一个具有相同名称的关键字并且与候选关键字相关联的值包含在与目标关键字相关联的值中时,候选对象才包含在目标对象中。
SELECT JSON_CONTAINS('{"name": "book", "price": 3.00, "number": 100}', '{"name": "book", "price": 3}');
+-------------------------------------------------------------------------------------------------+
| JSON_CONTAINS('{"name": "book", "price": 3.00, "number": 100}', '{"name": "book", "price": 3}') |
+-------------------------------------------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------------------------------------------+
到这里JSON_CONTAINS(target, candidate[, path])
的简单使用已经介绍完毕。