第一標題
1、JSON_ARRAY_APPEND數組類的追加
這個用法主要是處理JSON字段中值數組型的數據,主要是追加值,不是標準數組類型的,這個函數會自動轉換為數組形式,把先前的值作為數組的第一個元素然後進行追加,有批次追加方法:
mysql> SET @j = '["a", ["b", "c"], "d"]';
在字段中第二個數值的數組中追加一個元素1:
mysql> SELECT JSON_ARRAY_APPEND(@j, '$[1]', 1); +----------------------------------+ | JSON_ARRAY_APPEND(@j, '$[1]', 1) | +----------------------------------+ | ["a", ["b", "c", 1], "d"] | +----------------------------------+
在字段中第一個數值的數組中追加一個元素2,如果不是數組形式的單一值會改變其為數組並追加:
mysql> SELECT JSON_ARRAY_APPEND(@j, '$[0]', 2); +----------------------------------+ | JSON_ARRAY_APPEND(@j, '$[0]', 2) | +----------------------------------+ | [["a", 2], ["b", "c"], "d"] | +----------------------------------+
在字段中第二個數值的第一個數組中追加一個元素3,如果不是數組形式的單個數值會改變其為數組並追加:
mysql> SELECT JSON_ARRAY_APPEND(@j, '$[1][0]', 3); +-------------------------------------+ | JSON_ARRAY_APPEND(@j, '$[1][0]', 3) | +-------------------------------------+ | ["a", [["b", 3], "c"], "d"] | +-------------------------------------+
在字段中key為”b”的value數組中追加一個元素”x”:
mysql> SET @j = '{"a": 1, "b": [2, 3], "c": 4}'; mysql> SELECT JSON_ARRAY_APPEND(@j, '$.b', 'x'); +------------------------------------+ | JSON_ARRAY_APPEND(@j, '$.b', 'x') | +------------------------------------+ | {"a": 1, "b": [2, 3, "x"], "c": 4} | +------------------------------------+
”在字段中的中為為(數組中追加一個元素」y」:
mysql> SELECT JSON_ARRAY_APPEND(@j, '$.c', 'y'); +--------------------------------------+ | JSON_ARRAY_APPEND(@j, '$.c', 'y') | +--------------------------------------+ | {"a": 1, "b": [2, 3], "c": [4, "y"]} | +--------------------------------------+
mysql> SET @j = '{"a": 1}'; mysql> SELECT JSON_ARRAY_APPEND(@j, '$', 'z'); +---------------------------------+ | JSON_ARRAY_APPEND(@j, '$', 'z') | +---------------------------------+ | [{"a": 1}, "z"] | +---------------------------------+
mysql> SELECT JSON_ARRAY_APPEND(@j, '$', 'z','$', 'w'); +---------------------------------+ | JSON_ARRAY_APPEND(@j, '$', 'z') | +---------------------------------+ | [{"a": 1}, "z", "w"] | +---------------------------------+
mysql> SET @j = '["a", {"b": [1, 2]}, [3, 4]]';
mysql> SELECT JSON_ARRAY_INSERT(@j, '$[1]', 'x'); +------------------------------------+ | JSON_ARRAY_INSERT(@j, '$[1]', 'x') | +------------------------------------+ | ["a", "x", {"b": [1, 2]}, [3, 4]] |
mysql> SELECT JSON_ARRAY_INSERT(@j, '$[0]', 'x'); +------------------------------------+ | JSON_ARRAY_INSERT(@j, '$[1]', 'x') | +------------------------------------+ | ["x","a", {"b": [1, 2]}, [3, 4]] | +------------------------------------+
在數組第100個值後面插入字元'x' 元素,如果超長,則在最後一個值後面插入:
mysql> SELECT JSON_ARRAY_INSERT(@j, '$[100]', 'x'); +--------------------------------------+ | JSON_ARRAY_INSERT(@j, '$[100]', 'x') | +--------------------------------------+ | ["a", {"b": [1, 2]}, [3, 4], "x"] | +--------------------------------------+
mysql> SELECT JSON_ARRAY_INSERT(@j, '$[1].b[0]', 'x'); +-----------------------------------------+ | JSON_ARRAY_INSERT(@j, '$[1].b[0]', 'x') | +-----------------------------------------+ | ["a", {"b": ["x", 1, 2]}, [3, 4]] | +-----------------------------------------+
mysql> SELECT JSON_ARRAY_INSERT(@j, '$[2][1]', 'y'); +---------------------------------------+ | JSON_ARRAY_INSERT(@j, '$[2][1]', 'y') | +---------------------------------------+ | ["a", {"b": [1, 2]}, [3, "y", 4]] | +---------------------------------------+
mysql> SELECT JSON_ARRAY_INSERT(@j, '$[0]', 'x', '$[2][1]', 'y'); +----------------------------------------------------+ | JSON_ARRAY_INSERT(@j, '$[0]', 'x', '$[2][1]', 'y') | +----------------------------------------------------+ | ["x", "a", {"b": [1, 2]}, [3, 4]] | +----------------------------------------------------+
3、JSON_INSERT標準JSON值的插入如果這個key已經存在,則不會去覆蓋舊值,不存在的插入:
mysql> SET @j = '{ "a": 1, "b": [2, 3]}'; mysql> SELECT JSON_INSERT(@j, '$.a', 10, '$.c', '[true, false]'); +----------------------------------------------------+ | JSON_INSERT(@j, '$.a', 10, '$.c', '[true, false]') | +----------------------------------------------------+ | {"a": 1, "b": [2, 3], "c": "[true, false]"} | +----------------------------------------------------+
mysql> SELECT JSON_MERGE('[1, 2]', '[true, false]'); +---------------------------------------+ | JSON_MERGE('[1, 2]', '[true, false]') | +---------------------------------------+ | [1, 2, true, false] | +---------------------------------------+ mysql> SELECT JSON_MERGE('{"name": "x"}', '{"id": 47}'); +-------------------------------------------+ | JSON_MERGE('{"name": "x"}', '{"id": 47}') | +-------------------------------------------+ | {"id": 47, "name": "x"} | +-------------------------------------------+ mysql> SELECT JSON_MERGE('{"id": "x"}', '{"id": 47}'); +-------------------------------------------+ | JSON_MERGE('{"id": "x"}', '{"id": 47}') | +-------------------------------------------+ | {"id": ["x",47]} | +-------------------------------------------+ mysql> SELECT JSON_MERGE('1', 'true'); +-------------------------+ | JSON_MERGE('1', 'true') | +-------------------------+ | [1, true] | +-------------------------+ mysql> SELECT JSON_MERGE('[1, 2]', '{"id": 47}'); +------------------------------------+ | JSON_MERGE('[1, 2]', '{"id": 47}') | +------------------------------------+ | [1, 2, {"id": 47}] | +------------------------------------+
mysql> SET @j = '["a", ["b", "c"], "d"]'; mysql> SELECT JSON_REMOVE(@j, '$[1]'); +-------------------------+ | JSON_REMOVE(@j, '$[1]') | +-------------------------+ | ["a", "d"] | +-------------------------+ mysql> SET @j = '{"name": "x","id": 47}'; mysql> SELECT JSON_REMOVE(@j, '$.id'); +-------------------------+ | JSON_REMOVE(@j, '$.id') | +-------------------------+ | {"name": "x"} | +-------------------------+ mysql> SELECT JSON_REMOVE(@j, '$.id', '$.name'); +-----------------------------------+ | JSON_REMOVE(@j, '$.id', '$.name') | +-----------------------------------+ | {} | +-----------------------------------+ mysql> SET @j = '{"name": "x","id": [47,48]}'; mysql> SELECT JSON_REMOVE(@j, '$.id[1]'); +-------------------------+ | JSON_REMOVE(@j, '$.id') | +-------------------------+ | {"id": [47],"name": "x"} | +-------------------------+
mysql> SET @j = '{ "a": 1, "b": [2, 3]}'; mysql> SELECT JSON_REPLACE(@j, '$.a', 10, '$.c', '[true, false]'); +-----------------------------------------------------+ | JSON_REPLACE(@j, '$.a', 10, '$.c', '[true, false]') | +-----------------------------------------------------+ | {"a": 10, "b": [2, 3]} +-----------------------------------------------------+ mysql> SELECT JSON_REPLACE(@j, '$.a', 10, '$.b[1]',99); +-----------------------------------------------------+ | JSON_REPLACE(@j, '$.a', 10, '$.b[1]',99') | +-----------------------------------------------------+ | {"a": 10, "b": [2, 99]} | +-----------------------------------------------------+