我需要编写一个查询(或函数),它将使用存储hstore
在另一个表的列中的值更新表中的现有记录。例如:
create temp table foo(id int primary key, f1 int, f2 text, f3 int);
insert into foo values
(1, 1, 'jack', 1),
(2, 2, 'ted' , 2),
(3, 3, 'fred', 3);
create temp table bar(foo_id int references foo(id), row_data hstore);
insert into bar values
(1, 'f1=>0, f2=>bill'::hstore),
(2, 'f1=>0, f2=>will, f3=>0'::hstore),
(3, 'f3=>0'::hstore);
只有列中具有值的hstore
列才会更新,因此在处理之后,所需的结果将是:
select * from foo;
+----+----+------+----+
| id | f1 | f2 | f3 |
+----+----+------+----+
| 1 | 0 | bill | 1 |
| 2 | 0 | will | 0 |
| 3 | 3 | fred | 0 |
+----+----+------+----+
foo
用 中的值更新的“最佳”方法是bar
什么?
注意:我将best定义为最容易编码。虽然性能总是很重要,但这是一个批处理作业,速度并不像用户等待结果那样重要。
我正在使用 PostgreSQL 9.4。