Seb*_*mps 17 postgresql update postgresql-9.3 json
我不知道如何更新 PostgreSQL 9.3 数据类型中的元素。
我的例子:
CREATE TABLE "user"
(
id uuid NOT NULL,
password character varying(255),
profiles json,
gender integer NOT NULL DEFAULT 0,
created timestamp with time zone,
connected timestamp with time zone,
modified timestamp with time zone,
active integer NOT NULL DEFAULT 1,
settings json,
seo character varying(255) NOT NULL,
CONSTRAINT id_1 PRIMARY KEY (id)
)
WITH (
OIDS=TRUE
);
ALTER TABLE "user"
OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud)
“配置文件”中的 json 部分
CREATE TABLE "user"
(
id uuid NOT NULL,
password character varying(255),
profiles json,
gender integer NOT NULL DEFAULT 0,
created timestamp with time zone,
connected timestamp with time zone,
modified timestamp with time zone,
active integer NOT NULL DEFAULT 1,
settings json,
seo character varying(255) NOT NULL,
CONSTRAINT id_1 PRIMARY KEY (id)
)
WITH (
OIDS=TRUE
);
ALTER TABLE "user"
OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud)
我在前端使用 x-edit,我希望这样的事情会起作用,但它没有:
UPDATE public.user
SET "profiles"->'Facebook'->'social'->'facebook' = 'test' WHERE` id='id'
Run Code Online (Sandbox Code Playgroud)
我似乎找不到有关如何更新 json 数据类型的任何信息。
小智 9
由于它只是一个字符串,因此您可以使用该regex_replace函数完成节点的简单更改/删除。
例如,这是我最近删除表(所有行)中某个 JSON 节点的方式:
UPDATE my_db.my_table
SET my_column = (regexp_replace(my_column::text, ',"some_json_node":(.*),', ','))::json
WHERE NOT my_column IS NULL
Run Code Online (Sandbox Code Playgroud)
请注意,对于我的所有JSON 数据,我"version":"(n).(n)"在对象中保留了一个(即模式版本)节点。这样我就可以更新符合特定版本的对象。您的要求可能并不那么复杂,但如果确实如此,那肯定会有所帮助。
小智 5
尝试这个
UPDATE Tablename
SET columnname = replace(columnname::TEXT,'"name":','"my-other-name"')::jsonb
WHERE id = 1;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57348 次 |
| 最近记录: |