【发布时间】:2014-03-31 16:21:18
【问题描述】:
CREATE TABLE object (
object_id serial,
object_attribute_1 integer,
object_attribute_2 VARCHAR(255)
)
-- primary key object_id
-- btree index on object_attribute_1, object_attribute_2
这是我目前拥有的:
SELECT * FROM object
WHERE (object_attribute_1=100 AND object_attribute_2='Some String') OR
(object_attribute_1=200 AND object_attribute_2='Some other String') OR
(..another row..) OR
(..another row..)
当查询返回时,我会检查缺少的内容(因此,数据库中不存在)。
然后我将进行多行插入:
INSERT INTO object (object_attribute_1, object_attribute_2)
VALUES (info, info), (info, info),(info, info)
然后我会选择我刚刚插入的内容
SELECT ... WHERE (condition) OR (condition) OR ...
最后,我将在客户端合并两个选择。
有没有一种方法可以将这 3 个查询组合成一个查询,我将在其中提供所有数据,如果记录尚不存在,则提供 INSERT,然后在最后执行 SELECT。
【问题讨论】:
标签: sql postgresql common-table-expression sql-insert