我在 Postgres 9.5 中使用新的 UPSERT 功能时遇到问题
我有一个表,用于从另一个表聚合数据。复合键由 20 列组成,其中 10 列可以为空。下面我创建了我遇到的问题的较小版本,特别是 NULL 值。
CREATE TABLE public.test_upsert (
upsert_id serial,
name character varying(32) NOT NULL,
status integer NOT NULL,
test_field text,
identifier character varying(255),
count integer,
CONSTRAINT upsert_id_pkey PRIMARY KEY (upsert_id),
CONSTRAINT test_upsert_name_status_test_field_key UNIQUE (name, status, test_field)
);
Run Code Online (Sandbox Code Playgroud)
根据需要运行此查询(首先插入,然后插入只会增加计数):
INSERT INTO test_upsert as tu(name,status,test_field,identifier, count)
VALUES ('shaun',1,'test value','ident', 1)
ON CONFLICT (name,status,test_field) DO UPDATE set count = tu.count + 1
where tu.name = 'shaun' AND tu.status = 1 AND tu.test_field = …Run Code Online (Sandbox Code Playgroud)