Postgresql数据库操作

修改Jsonb中某个键值 例如 如果为 1 则修改成男 0则为女

UPDATE "large_screen"."overall_statistics"
SET statistics_data = 
  CASE  
		 WHEN statistics_data->>'fault_type' = '故障' THEN jsonb_set(statistics_data, '{fault_type}', '"fault"')
		 WHEN statistics_data->>'fault_type' = '正常运行' THEN jsonb_set(statistics_data, '{fault_type}', '"normalOperation"')
     ELSE statistics_data
  END
WHERE 
  (statistics_data->>'fault_type' IN ('正常运行', '故障') OR (statistics_data->>'fault_type' IS NULL AND statistics_count = 0))
  AND jsonb_typeof(statistics_data) = 'object' and statistics_type = 'offline'

两表 将副表某些字段改成JsonB字段另起一列

Postgresql数据库操作_第1张图片

// attributes就是新生成的字段,
// attribute_kv 表中的attribute_key和long_v字段变成 {"state": 2, "online": 1}

SELECT
    dev.id,
    jsonb_object_agg(att.attribute_key, att.long_v) AS attributes
FROM
    "public".device dev
LEFT JOIN
    "public".attribute_kv att ON dev."id" = att.entity_id 
WHERE
    att.attribute_key IN ('state','online' )
GROUP BY
    dev.id;

你可能感兴趣的:(数据库,postgresql,java)