我需要在colunm收藏夹中更新数据:
CREATE TABLE IF NOT EXISTS client
(
id text UNIQUE NOT NULL
CONSTRAINT crm_id_pkey PRIMARY KEY,
favorites jsonb default '{}'
);
值看起来像这样:
INSERT INTO client (id, favorites)
VALUES ('123',
'{
"111111": "2018-08-06 11:07:05.710000",
"222222": "2019-08-06 11:07:05.710000",
"333333": "2019-06-06 11:07:05.710000"
}');
我在github上找到了一个非常有用的页面,给定两个json,有一些sql脚本可以进行合并或更新。如果您愿意,可以选中此link!
我尝试执行以下代码:
WITH data AS (
SELECT json_merge(fav.favorites::json, '{"111111": "2000-00-00 11:07:05.710000","123123": "1999-00-00 11:07:05.710000"}'::json)::json
FROM mobappb2c_profile.client_material as fav
WHERE crm_id = '123')
SELECT
sku2date.key AS sku
FROM data AS fav,
to_jsonb(fav) as js,
jsonb_each(js) AS sku2date
order by sku2date.value desc;
所以我想合并一些json,合并后我需要选择所有键,但是当我第二次选择jsonb_each(...我得到结果值:
sku
----------
json_merge
怎么可能?
我发现函数json_merge
并不是以这种方式插入数据,所以我很困惑,我需要帮助