相关文章推荐
贪玩的上铺  ·  Windows ...·  1 年前    · 
踏实的移动电源  ·  gulp ...·  2 年前    · 

I have a nullable JSON MySQL 5.7 field which I am finding almost impossible to get working.

Example query:

UPDATE `json_test` SET `data` = JSON_SET(`data`, '$.a', 1)

If the field data is NULL already, it won't update.

If it's { "a" : 2 }, then it'll update correctly to 1. I need it to set if not set already, which is what JSON_SET is supposed to do.

Any ideas what's happening?

it's not supposed to work with nulls

Otherwise, a path/value pair for a nonexisting path in the document is

ignored and has no effect.

Now mysql doesn't let you use a subquery on the same table that's being updated, but you could probably stil solve this with an UPDATE JOIN using CASE/WHEN but I am too lazy so I leave you with a two query solution.

UPDATE `json_test` SET `data` = JSON_SET(`data`, '$.a', 1) WHERE data IS NOT NULL;

UPDATE `json_test` SET `data` = JSON_OBJECT('$.a', 1) WHERE data IS NULL;

I have a nullable JSON MySQL 5.7 field which I am finding almost impossible to get working.Example query:UPDATE `json_test` SET `data` = JSON_SET(`data`, '$.a', 1)If the field data is NULL already, it... 1、拷贝lib_ mysql udf_ json _parsing.dll到 mysql 目录C:\Program Files\MariaDB 5.5\lib\plugin下 2、在数据库 执行 DROP FUNCTION json _get; CREATE FUNCTION json _get RETURNS STRING SONAME 'lib_ mysql udf_ json _parsing.dll'; SELECT json _get('{"a":1}', 'a') => 1 SELECT json _get('{"a":1}', 'b') => NULL SELECT json _get('[1,2,3]', 2) => 3 SELECT json _get('{"a":[2]}', 'a', 0) => 2 "count": 1, "skuId": 50, "picture": "http://img.alicdn.com/bao/uploaded/i1/2455464663/TB1.u3oSFXXXXbDXFXXXXXXXXXX_!!0-item_pic.jpg", "realPrice": 0.1, "attribute.
MySQL 有一种数据类型为 json 类型 查询 json 字段 的话一般的 字段 名= null 或者 字段 名="" 以及 字段 名=CAST( NULL AS JSON ) 都查不出结果的 JSON _EXTRACT(Tags, ‘$[0]’) IS NULL 附带一个案例
MySQL ,您可以使用函数 ` json _extract` 处理 JSON 类型的数据。该函数接受两个参数:第一个参数是包含 JSON 数据的列,第二个参数是用于提取 JSON 数据的路径表达式。 例如,假设有一张名为 `mytable` 的表格,其 有一个名为 `mycolumn` 的列包含以下 JSON 数据: ``` json "name": "John Smith", "age": 30, "address": { "street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345" 要提取此 JSON 数据 的 `name` 字段 ,您可以使用以下查询: ```sql SELECT json _extract(mycolumn, '$.name') FROM mytable; 此查询将返回 `John Smith`。要提取地址信息,您可以使用以下查询: ```sql SELECT json _extract(mycolumn, '$.address') FROM mytable; 此查询将返回以下 JSON 数据: ``` json "street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345" 希望这可以帮助您开始使用 ` json _extract` 函数 处理 MySQL JSON 数据。