1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
mysql> select json_extract('[10, 20, [30, 40]]', '$[1]'); + | json_extract('[10, 20, [30, 40]]', '$[1]') | + | 20 | + 1 row in set (0.00 sec)
mysql> select -> json_extract(data, '$.name'), -> json_extract(data, '$.address') -> from user; + | json_extract(data, '$.name') | json_extract(data, '$.address') | + | "tom" | "SZ" | | "jim" | NULL | + 2 rows in set (0.00 sec)
mysql> select json_object("name", "jery", "email", "jery@163.com", "age",33); + | json_object("name", "jery", "email", "jery@163.com", "age",33) | + | {"age": 33, "name": "jery", "email": "jery@163.com"} | -- 封装成了K-V对 + 1 row in set (0.00 sec)
mysql> insert into user values ( -> null, -> json_object("name", "jery", "email", "jery@163.com", "age",33) -> ); Query OK, 1 row affected (0.03 sec)
mysql> select * from user; + | uid | data | + | 1 | {"age": 18, "name": "tom", "address": "SZ"} | | 2 | {"age": 28, "mail": "jim@163.com", "name": "jim"} | | 4 | {"age": 33, "name": "jery", "email": "jery@163.com"} | + 3 rows in set (0.00 sec)
mysql> set @j = '{ "a": 1, "b": [2, 3]}'; Query OK, 0 rows affected (0.00 sec)
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]"} | -- a还是=1,存在的被忽略,不受影响 + 1 row in set (0.00 sec)
mysql> update user set data = json_insert(data, "$.address_2", "BJ") where uid = 1; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user; + | uid | data | + | 1 | {"age": 18, "name": "tom", "address": "SZ", "address_2": "BJ"} | -- 增加了addres_2 : "BJ" | 2 | {"age": 28, "mail": "jim@163.com", "name": "jim"} | | 4 | {"age": 33, "name": "jery", "email": "jery@163.com"} | + 3 rows in set (0.00 sec)
mysql> select json_merge('{"name": "x"}', '{"id": 47}'); + | json_merge('{"name": "x"}', '{"id": 47}') | + | {"id": 47, "name": "x"} | -- 合并多个JSON + 1 row in set (0.00 sec)
mysql> select -> json_merge( -> json_extract(data, '$.address'), -> json_extract(data, '$.address_2')) -> from user where uid = 1; + | json_merge( json_extract(data, '$.address'), json_extract(data, '$.address_2')) | + | ["SZ", "BJ"] | + 1 row in set (0.00 sec)
mysql> set @j = '["a", ["b", "c"], "d"]'; Query OK, 0 rows affected (0.00 sec)
mysql> select json_array_append(@j, '$[1]', 1); + | json_array_append(@j, '$[1]', 1) | + | ["a", ["b", "c", 1], "d"] | + 1 row in set (0.00 sec) mysql> update user set data = json_array_append( -> data, -> '$.address', -> json_extract(data, '$.address_2')) -> where uid = 1; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user; + | uid | data | + | 1 | {"age": 18, "name": "tom", "address": ["SZ", "BJ"], "address_2": "BJ"} | --address_2追加到address | 2 | {"age": 28, "mail": "jim@163.com", "name": "jim"} | | 4 | {"age": 33, "name": "jery", "email": "jery@163.com"} | + 3 rows in set (0.00 sec)
mysql> set @j = '["a", ["b", "c"], "d"]'; Query OK, 0 rows affected (0.00 sec)
mysql> select json_remove(@j, '$[1]'); + | json_remove(@j, '$[1]') | + | ["a", "d"] | + 1 row in set (0.00 sec)
mysql> update user set data = json_remove(data, "$.address_2") where uid = 1; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user; + | uid | data | + | 1 | {"age": 18, "name": "tom", "address": ["SZ", "BJ"]} | -- address_2 的字段删除了 | 2 | {"age": 28, "mail": "jim@163.com", "name": "jim"} | | 4 | {"age": 33, "name": "jery", "email": "jery@163.com"} | + 3 rows in set (0.00 sec)
|