mysql 5.7 json 字段類型查找、修改

修改 json 裏的數組字段php

mysql> set @json = '{"test": [{"name": "laravel"}, {"name": "symfony"}]}';
Query OK, 0 rows affected (0.00 sec)
 
mysql> select json_set(@json, '$.test[0].name', "lumen");
+----------------------------------------------------+
| json_set(@json, '$.test[0].name', "lumen")         |
+----------------------------------------------------+
| {"test": [{"name": "lumen"}, {"name": "symfony"}]} |
+----------------------------------------------------+
1 row in set (0.00 sec)

  

select、匹配 json 字段html

SELECT JSON_EXTRACT(name, "$.id") AS name
FROM table
WHERE JSON_EXTRACT(name, "$.id") > 3

  

json 字段名有橫槓的處理:給字段名加雙引號mysql

select json_set(@json, '$."just-test".name', "lumen");

  

設置值爲數組:使用 json_array 函數laravel

set @json = json_set(@json, '$."dash-test"', json_array(1));

  

設置值爲對象:使用 json_object 函數sql

mysql> set @json = json_set(@json, '$.set_test[0]', json_object('name', 'awks'));
Query OK, 0 rows affected (0.00 sec)
 
mysql> select @json;
+-------------------------------------------------------------------------------------------------------+
| @json                                                                                                 |
+-------------------------------------------------------------------------------------------------------+
| {"test": [{"name": "laravel"}, {"name": "symfony"}], "set_test": [{"name": "awks"}], "dash-test": []} |
+-------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> 

 

更多參考資料:json

json 字段數據類型:https://dev.mysql.com/doc/refman/5.7/en/json.html數組

json 相關操做函數:https://dev.mysql.com/doc/refman/5.7/en/json-functions.html函數

相關文章
相關標籤/搜索