官方文档mysql 5.7 json
官方文档mysql 5.7 json functions
Name | Description | |
---|---|---|
-> | Return value from JSON column after evaluating path; equivalent to JSON_EXTRACT(). | |
->>?(introduced 5.7.13) | Return value from JSON column after evaluating path and unquoting the result; equivalent to JSON_UNQUOTE(JSON_EXTRACT()). | |
JSON_APPEND()?(deprecated) | Append data to JSON document | |
JSON_ARRAY() | Create JSON array | |
JSON_ARRAY_APPEND() | Append data to JSON document | |
JSON_ARRAY_INSERT() | Insert into JSON array | |
JSON_CONTAINS() | Whether JSON document contains specific object at path | |
JSON_CONTAINS_PATH() | Whether JSON document contains any data at path | |
JSON_DEPTH() | Maximum depth of JSON document | |
JSON_EXTRACT() | Return data from JSON document | |
JSON_INSERT() | Insert data into JSON document | |
JSON_KEYS() | Array of keys from JSON document | |
JSON_LENGTH() | Number of elements in JSON document | |
JSON_MERGE()?(deprecated 5.7.22) | “Merge JSON documents | preserving duplicate keys. Deprecated synonym for JSON_MERGE_PRESERVE()” |
JSON_MERGE_PATCH()?(introduced 5.7.22) | “Merge JSON documents | replacing values of duplicate keys” |
JSON_MERGE_PRESERVE()?(introduced 5.7.22) | “Merge JSON documents | preserving duplicate keys” |
JSON_OBJECT() | Create JSON object | |
JSON_PRETTY()?(introduced 5.7.22) | Print a JSON document in human-readable format | |
JSON_QUOTE() | Quote JSON document | |
JSON_REMOVE() | Remove data from JSON document | |
JSON_REPLACE() | Replace values in JSON document | |
JSON_SEARCH() | Path to value within JSON document | |
JSON_SET() | Insert data into JSON document | |
JSON_STORAGE_SIZE()?(introduced 5.7.22) | Space used for storage of binary representation of a JSON document | |
JSON_TYPE() | Type of JSON value | |
JSON_UNQUOTE() | Unquote JSON value | |
JSON_VALID() | Whether JSON value is valid |
JSON_ARRAY json数组
1 | mysql> SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()); |
JSON_OBJECT
1 | mysql> SELECT JSON_OBJECT('key1', 1, 'key2', 'abc'); |
JSON_EXTRACT 查询json列 同 column->path
1 | mysql> SELECT JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name'); |
column->>path
等价于
JSON_UNQUOTE( JSON_EXTRACT(column, path) )
JSON_UNQUOTE(column -> path)
column->>path
1 | mysql> SELECT * FROM jemp WHERE g > 2; |
JSON_TYPE
1 | mysql> SELECT JSON_TYPE('["a", "b", 1]'); |
JSON_MERGE
1 | mysql> SELECT JSON_MERGE('["a", 1]', '{"key": "value"}'); |
JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] …])
如果json_doc,search_str或path参数中的任何一个为NULL,则返回NULL;否则返回NULL。文档内没有路径;或找不到search_str。
如果json_doc参数不是有效的JSON文档,任何路径参数不是有效的路径表达式,one_or_all不是’one’或’all’或escape_char不是常量表达式,则会发生错误。
要在搜索字符串中指定文字%或_字符,请在其前面加上转义字符。如果escape_char参数丢失或为NULL,则默认值为\。否则,scape_char必须为空或一个字符的常量。
search_str: % 匹配任意的字符(含0), _ 完全匹配一个字符
如果在准备好的语句中使用了JSON_SEARCH(),并且使用?提供了escape_char参数?参数,参数值在执行时可能是恒定的,但在编译时却不是。
1 | mysql> SET @j = '["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]'; |
JSON_CONTAINS(target, candidate[, path])
1 | mysql> SET @j = '{"a": 1, "b": 2, "c": {"d": 4}}'; |
JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] …)
one’: 1 if at least one path exists within the document, 0 otherwise.
‘all’: 1 if all paths exist within the document, 0 otherwise.
1 | mysql> SET @j = '{"a": 1, "b": 2, "c": {"d": 4}}'; |
JSON_SET() replaces existing values and adds nonexisting values.
JSON_INSERT() inserts values without replacing existing values.
JSON_REPLACE() replaces only existing values.
1 | mysql> SET @j = '{ "a": 1, "b": [2, 3]}'; |