select * from nw_wechat_order where JSON_CONTAINS(goods_info->'$[*].title', '"秋裤2"', '$') or goods_info->'$.title' = '秋裤2'
JSON_CONTAINS(target, candidate[, path])
通过返回1或0来指示给定的 candidateJSON文档是否包含在targetJSON文档中,或者 - 如果提供了path 参数 - 是否在目标内的特定路径中找到候选项。返回 NULL如果任何参数 NULL,或者如果路径参数不识别目标文档的一部分。如果发生错误 target或 candidate不是有效的JSON文档,或者如果path参数不是一个有效的路径表达式或包含一个*或**通配符。
要仅检查路径中是否存在任何数据,请 JSON_CONTAINS_PATH()改用。
以下规则定义了遏制:
候选标量包含在目标标量中,当且仅当它们具有可比性且相等时。如果它们具有相同的JSON_TYPE()类型,则两个标量值是可比较的 ,除了类型的值INTEGER 并且DECIMAL也彼此相当。
当且仅当候选中的每个元素都包含在目标的某个元素中时,候选数组才包含在目标数组中。
当且仅当候选者包含在目标的某个元素中时,候选非阵列包含在目标数组中。
候选对象包含在目标对象中,当且仅当对于候选中的每个键时,在目标中存在具有相同名称的键,并且与候选键相关联的值包含在与目标键相关联的值中。
否则,候选值不包含在目标文档中。
mysql>SET@j='{"a": 1, "b": 2, "c": {"d": 4}}';mysql>SET@j2='1';mysql>SELECTJSON_CONTAINS(@j,@j2,'$.a');+-------------------------------+|JSON_CONTAINS(@j, @j2, '$.a')|+-------------------------------+|1|+-------------------------------+mysql>SELECTJSON_CONTAINS(@j,@j2,'$.b');+-------------------------------+|JSON_CONTAINS(@j, @j2, '$.b')|+-------------------------------+|0|+-------------------------------+mysql>SET@j2='{"d": 4}';mysql>SELECTJSON_CONTAINS(@j,@j2,'$.a');+-------------------------------+|JSON_CONTAINS(@j, @j2, '$.a')|+-------------------------------+|0|+-------------------------------+mysql>SELECTJSON_CONTAINS(@j,@j2,'$.c');+-------------------------------+|JSON_CONTAINS(@j, @j2, '$.c')|+-------------------------------+|1|+-------------------------------+
JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
返回0或1以指示JSON文档是否包含给定路径或路径的数据。NULL 如果有任何参数,则返回NULL。如果json_doc参数不是有效的JSON文档,任何path 参数不是有效的路径表达式,或者one_or_all不是 'one'或, 则会发生错误'all'。
要检查路径中的特定值,请 JSON_CONTAINS()改用。
如果文档中不存在指定的路径,则返回值为0。否则,返回值取决于 one_or_all参数:
'one':如果文档中至少存在一个路径,则为1,否则为0。
'all':如果文档中存在所有路径,则为1,否则为0。
mysql>SET@j='{"a": 1, "b": 2, "c": {"d": 4}}';mysql>SELECTJSON_CONTAINS_PATH(@j,'one','$.a','$.e');+---------------------------------------------+|JSON_CONTAINS_PATH(@j, 'one', '$.a', '$.e')|+---------------------------------------------+|1|+---------------------------------------------+mysql>SELECTJSON_CONTAINS_PATH(@j,'all','$.a','$.e');+---------------------------------------------+|JSON_CONTAINS_PATH(@j, 'all', '$.a', '$.e')|+---------------------------------------------+|0|+---------------------------------------------+mysql>SELECTJSON_CONTAINS_PATH(@j,'one','$.c.d');+----------------------------------------+|JSON_CONTAINS_PATH(@j, 'one', '$.c.d')|+----------------------------------------+|1|+----------------------------------------+mysql>SELECTJSON_CONTAINS_PATH(@j,'one','$.a.d');+----------------------------------------+|JSON_CONTAINS_PATH(@j, 'one', '$.a.d')|+----------------------------------------+|0|+----------------------------------------+
JSON_EXTRACT(json_doc, path[, path] ...)
返回JSON文档中的数据,该文档从path 参数匹配的文档部分中选择。返回NULL如果任何参数 NULL或没有路径找到文档中的一个值。如果json_doc参数不是有效的JSON文档或任何path参数不是有效的路径表达式,则会发生错误 。
返回值由path参数匹配的所有值组成 。如果这些参数可能返回多个值,则匹配的值将按照与生成它们的路径对应的顺序自动包装为数组。否则,返回值是单个匹配的值。
mysql>SELECTJSON_EXTRACT('[10, 20, [30, 40]]','$[1]');+--------------------------------------------+|JSON_EXTRACT('[10, 20, [30, 40]]', '$[1]')|+--------------------------------------------+|20|+--------------------------------------------+mysql>SELECTJSON_EXTRACT('[10, 20, [30, 40]]','$[1]','$[0]');+----------------------------------------------------+|JSON_EXTRACT('[10, 20, [30, 40]]', '$[1]', '$[0]')|+----------------------------------------------------+|[20, 10]|+----------------------------------------------------+mysql>SELECTJSON_EXTRACT('[10, 20, [30, 40]]','$[2][*]');+-----------------------------------------------+|JSON_EXTRACT('[10, 20, [30, 40]]', '$[2][*]')|+-----------------------------------------------+|[30, 40]|+-----------------------------------------------+
MySQL 5.7.9及更高版本支持 -> 运算符作为此函数的简写,与2个参数一起使用,其中左侧是 JSON列标识符(不是表达式),右侧是要在列中匹配的JSON路径。
column->path
在MySQL 5.7.9及更高版本中,当与两个参数一起使用时, -> 运算符用作JSON_EXTRACT()函数的别名, 左边是列标识符,右边是JSON路径,根据JSON文档(列值)计算。您可以使用此类表达式代替SQL语句中出现的列标识符。
SELECT这里显示 的两个语句产生相同的输出:
mysql>SELECTc,JSON_EXTRACT(c,"$.id"),g>FROMjemp>WHEREJSON_EXTRACT(c,"$.id")>1>ORDERBYJSON_EXTRACT(c,"$.name");+-------------------------------+-----------+------+|c|c->"$.id"|g|+-------------------------------+-----------+------+|{"id": "3", "name": "Barney"}|"3"|3||{"id": "4", "name": "Betty"}|"4"|4||{"id": "2", "name": "Wilma"}|"2"|2|+-------------------------------+-----------+------+3 rows in set (0.00 sec)mysql>SELECTc,c->"$.id",g>FROMjemp>WHEREc->"$.id">1>ORDERBYc->"$.name";+-------------------------------+-----------+------+|c|c->"$.id"|g|+-------------------------------+-----------+------+|{"id": "3", "name": "Barney"}|"3"|3||{"id": "4", "name": "Betty"}|"4"|4||{"id": "2", "name": "Wilma"}|"2"|2|+-------------------------------+-----------+------+3 rows in set (0.00 sec)
此功能不限 SELECT于此,如下所示:
mysql>ALTERTABLEjempADDCOLUMNnINT;Query OK, 0 rows affected (0.68 sec)Records: 0 Duplicates: 0 Warnings: 0mysql>UPDATEjempSETn=1WHEREc->"$.id"="4";Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql>SELECTc,c->"$.id",g,n>FROMjemp>WHEREJSON_EXTRACT(c,"$.id")>1>ORDERBYc->"$.name";+-------------------------------+-----------+------+------+|c|c->"$.id"|g|n|+-------------------------------+-----------+------+------+|{"id": "3", "name": "Barney"}|"3"|3|NULL||{"id": "4", "name": "Betty"}|"4"|4|1||{"id": "2", "name": "Wilma"}|"2"|2|NULL|+-------------------------------+-----------+------+------+3 rows in set (0.00 sec)mysql>DELETEFROMjempWHEREc->"$.id"="4";Query OK, 1 row affected (0.04 sec)mysql>SELECTc,c->"$.id",g,n>FROMjemp>WHEREJSON_EXTRACT(c,"$.id")>1>ORDERBYc->"$.name";+-------------------------------+-----------+------+------+|c|c->"$.id"|g|n|+-------------------------------+-----------+------+------+|{"id": "3", "name": "Barney"}|"3"|3|NULL||{"id": "2", "name": "Wilma"}|"2"|2|NULL|+-------------------------------+-----------+------+------+2 rows in set (0.00 sec)
(有关用于创建和填充刚刚显示的表的语句,请参阅索引生成的列以提供JSON列索引。)
这也适用于JSON数组值,如下所示:
mysql>CREATETABLEtj10(aJSON,bINT);Query OK, 0 rows affected (0.26 sec)mysql>INSERTINTOtj10>VALUES("[3,10,5,17,44]",33),("[3,10,5,17,[22,44,66]]",0);Query OK, 1 row affected (0.04 sec)mysql>SELECTa->"$[4]"FROMtj10;+--------------+|a->"$[4]"|+--------------+|44||[22, 44, 66]|+--------------+2 rows in set (0.00 sec)mysql>SELECT*FROMtj10WHEREa->"$[0]"=3;+------------------------------+------+|a|b|+------------------------------+------+|[3, 10, 5, 17, 44]|33||[3, 10, 5, 17, [22, 44, 66]]|0|+------------------------------+------+2 rows in set (0.00 sec)
支持嵌套数组。使用的表达式 ->计算NULL 好像在目标JSON文档中找不到匹配的键,如下所示:
mysql>SELECT*FROMtj10WHEREa->"$[4][1]"ISNOTNULL;+------------------------------+------+|a|b|+------------------------------+------+|[3, 10, 5, 17, [22, 44, 66]]|0|+------------------------------+------+mysql>SELECTa->"$[4][1]"FROMtj10;+--------------+|a->"$[4][1]"|+--------------+|NULL||44|+--------------+2 rows in set (0.00 sec)
这与使用时的情况相同 JSON_EXTRACT():
mysql>SELECTJSON_EXTRACT(a,"$[4][1]")FROMtj10;+----------------------------+|JSON_EXTRACT(a, "$[4][1]")|+----------------------------+|NULL||44|+----------------------------+2 rows in set (0.00 sec)
column->>path
这是一个改进的,不引用的提取操作符,可在MySQL 5.7.13及更高版本中使用。而 ->操作者简单地提取的值时, ->>在加法运算unquotes所提取的结果。换句话说,给定 JSON列值 column和路径表达式 path,以下三个表达式返回相同的值:
JSON_UNQUOTE( JSON_EXTRACT(column, path) )
JSON_UNQUOTE(column -> path)
column->>path
该->>操作可用于任何 JSON_UNQUOTE(JSON_EXTRACT())将被允许。这包括(但不限于) SELECT列表,WHERE和 HAVING条款,并ORDER BY和GROUP BY条款。
接下来的几个语句演示->>了mysql客户端中其他表达式的一些 运算符等价:
mysql>SELECT*FROMjempWHEREg>2;+-------------------------------+------+|c|g|+-------------------------------+------+|{"id": "3", "name": "Barney"}|3||{"id": "4", "name": "Betty"}|4|+-------------------------------+------+2 rows in set (0.01 sec)mysql>SELECTc->'$.name'ASname ->FROMjempWHEREg>2;+----------+|name|+----------+|"Barney"||"Betty"|+----------+2 rows in set (0.00 sec)mysql>SELECTJSON_UNQUOTE(c->'$.name')ASname ->FROMjempWHEREg>2;+--------+|name|+--------+|Barney||Betty|+--------+2 rows in set (0.00 sec)mysql>SELECTc->>'$.name'ASname ->FROMjempWHEREg>2;+--------+|name|+--------+|Barney||Betty|+--------+2 rows in set (0.00 sec)
请参阅索引生成的列以提供JSON列索引,用于在jemp刚刚显示的示例集中创建和填充表的SQL语句 。
此运算符也可以与JSON数组一起使用,如下所示:
mysql>CREATETABLEtj10(aJSON,bINT);Query OK, 0 rows affected (0.26 sec)mysql>INSERTINTOtj10VALUES ->('[3,10,5,"x",44]',33), ->('[3,10,5,17,[22,"y",66]]',0);Query OK, 2 rows affected (0.04 sec)Records: 2 Duplicates: 0 Warnings: 0mysql>SELECTa->"$[3]",a->"$[4][1]"FROMtj10;+-----------+--------------+|a->"$[3]"|a->"$[4][1]"|+-----------+--------------+|"x"|NULL||17|"y"|+-----------+--------------+2 rows in set (0.00 sec)mysql>SELECTa->>"$[3]",a->>"$[4][1]"FROMtj10;+------------+---------------+|a->>"$[3]"|a->>"$[4][1]"|+------------+---------------+|x|NULL||17|y|+------------+---------------+2 rows in set (0.00 sec)
与此同时 ->,->>运算符总是在输出中展开EXPLAIN,如下例所示:
mysql>EXPLAINSELECTc->>'$.name'ASname ->FROMjempWHEREg>2\G***************************1. row***************************id:1 select_type:SIMPLE table:jemp partitions:NULL type:rangepossible_keys:i key:i key_len:5 ref:NULL rows:2 filtered:100.00 Extra:Using where1 row in set, 1 warning (0.00 sec)mysql>SHOWWARNINGS\G***************************1. row***************************Level:Note Code:1003Message:/*select#1*/ selectjson_unquote(json_extract(`jtest`.`jemp`.`c`,'$.name')) AS `name` from`jtest`.`jemp` where (`jtest`.`jemp`.`g` > 2)1 row in set (0.00 sec)
这类似于MySQL -> 在相同情况下扩展 运算符的方式。
该->>操作符已添加到MySQL 5.7.13中。
JSON_KEYS(json_doc[, path])
从JSON对象的顶级值返回键作为JSON数组,或者,如果path 给出参数,则返回所选路径中的顶级键。NULL如果有任何参数NULL,则返回参数 ,该 json_doc参数不是对象,或者path,如果给定,则不返回对象。如果json_doc参数不是有效的JSON文档或path参数不是有效的路径表达式或包含 *或**通配符,则会发生错误 。
如果所选对象为空,则结果数组为空。如果顶级值具有嵌套的子对象,则返回值不包括来自这些子对象的键。
mysql>SELECTJSON_KEYS('{"a": 1, "b": {"c": 30}}');+---------------------------------------+|JSON_KEYS('{"a": 1, "b": {"c": 30}}')|+---------------------------------------+|["a", "b"]|+---------------------------------------+mysql>SELECTJSON_KEYS('{"a": 1, "b": {"c": 30}}','$.b');+----------------------------------------------+|JSON_KEYS('{"a": 1, "b": {"c": 30}}', '$.b')|+----------------------------------------------+|["c"]|+----------------------------------------------+
JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] ...])
返回JSON文档中给定字符串的路径。NULL如果任何,或者 参数是 json_doc, 则 返回; 文件中不存在; 或者 没找到。发生错误时,如果参数不是一个有效的JSON文档,任何 参数不是一个有效的路径表达式, 是不 或,或者 是不是一个常量表达式。 search_strpathNULLpathsearch_strjson_docpathone_or_all'one''all'escape_char
该one_or_all参数影响搜索,如下所示:
'one':搜索在第一个匹配后终止,并返回一个路径字符串。未定义哪个匹配首先考虑。
'all':搜索返回所有匹配的路径字符串,以便不包含重复的路径。如果有多个字符串,则将它们自动包装为数组。数组元素的顺序未定义。
在search_str搜索字符串参数中,%和_ 字符用于LIKE 运算符:%匹配任意数量的字符(包括零个字符),并且只 _匹配一个字符。
To specify a literal % or _ character in the search string, precede it by the escape character. The default is \ if the escape_char argument is missing or NULL. Otherwise,escape_char must be a constant that is empty or one character.
For more information about matching and escape character behavior, see the description of LIKE in Section 12.5.1, “String Comparison Functions and Operators”. For escape character handling, a difference from the LIKE behavior is that the escape character for JSON_SEARCH() must evaluate to a constant at compile time, not just at execution time. For example, if JSON_SEARCH() is used in a prepared statement and the escape_char argument is supplied using a ? parameter, the parameter value might be constant at execution time, but is not at compile time.
mysql>SET@j='["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]';mysql>SELECTJSON_SEARCH(@j,'one','abc');+-------------------------------+|JSON_SEARCH(@j, 'one', 'abc')|+-------------------------------+|"$[0]"|+-------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','abc');+-------------------------------+|JSON_SEARCH(@j, 'all', 'abc')|+-------------------------------+|["$[0]", "$[2].x"]|+-------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','ghi');+-------------------------------+|JSON_SEARCH(@j, 'all', 'ghi')|+-------------------------------+|NULL|+-------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','10');+------------------------------+|JSON_SEARCH(@j, 'all', '10')|+------------------------------+|"$[1][0].k"|+------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','10',NULL,'$');+-----------------------------------------+|JSON_SEARCH(@j, 'all', '10', NULL, '$')|+-----------------------------------------+|"$[1][0].k"|+-----------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','10',NULL,'$[*]');+--------------------------------------------+|JSON_SEARCH(@j, 'all', '10', NULL, '$[*]')|+--------------------------------------------+|"$[1][0].k"|+--------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','10',NULL,'$**.k');+---------------------------------------------+|JSON_SEARCH(@j, 'all', '10', NULL, '$**.k')|+---------------------------------------------+|"$[1][0].k"|+---------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','10',NULL,'$[*][0].k');+-------------------------------------------------+|JSON_SEARCH(@j, 'all', '10', NULL, '$[*][0].k')|+-------------------------------------------------+|"$[1][0].k"|+-------------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','10',NULL,'$[1]');+--------------------------------------------+|JSON_SEARCH(@j, 'all', '10', NULL, '$[1]')|+--------------------------------------------+|"$[1][0].k"|+--------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','10',NULL,'$[1][0]');+-----------------------------------------------+|JSON_SEARCH(@j, 'all', '10', NULL, '$[1][0]')|+-----------------------------------------------+|"$[1][0].k"|+-----------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','abc',NULL,'$[2]');+---------------------------------------------+|JSON_SEARCH(@j, 'all', 'abc', NULL, '$[2]')|+---------------------------------------------+|"$[2].x"|+---------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','%a%');+-------------------------------+|JSON_SEARCH(@j, 'all', '%a%')|+-------------------------------+|["$[0]", "$[2].x"]|+-------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','%b%');+-------------------------------+|JSON_SEARCH(@j, 'all', '%b%')|+-------------------------------+|["$[0]", "$[2].x", "$[3].y"]|+-------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','%b%',NULL,'$[0]');+---------------------------------------------+|JSON_SEARCH(@j, 'all', '%b%', NULL, '$[0]')|+---------------------------------------------+|"$[0]"|+---------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','%b%',NULL,'$[2]');+---------------------------------------------+|JSON_SEARCH(@j, 'all', '%b%', NULL, '$[2]')|+---------------------------------------------+|"$[2].x"|+---------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','%b%',NULL,'$[1]');+---------------------------------------------+|JSON_SEARCH(@j, 'all', '%b%', NULL, '$[1]')|+---------------------------------------------+|NULL|+---------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','%b%','','$[1]');+-------------------------------------------+|JSON_SEARCH(@j, 'all', '%b%', '', '$[1]')|+-------------------------------------------+|NULL|+-------------------------------------------+mysql>SELECTJSON_SEARCH(@j,'all','%b%','','$[3]');+-------------------------------------------+|JSON_SEARCH(@j, 'all', '%b%', '', '$[3]')|+-------------------------------------------+|"$[3].y"|+-------------------------------------------+