PG-承诺快速删除元组集的方法



有没有更好的方法来编写以下内容:

DELETE FROM
table
WHERE
_id = $<id>
AND (
item_id = $<item_id>
AND map_id = $<map_id>
OR
item_id = $<another_id>
AND map_id = $<another_map_id>
...
)

我拥有的数据集格式是:

[
{
item_id: "some_id",
map_id: "other_id"
}
...
]

pg-promise写这个,甚至只是普通的postgreSQL的好方法是什么?

DELETE FROM table
WHERE
_id = $<id>
AND (item_id, map_id) IN ($<values:raw>)

如果您有以下内容作为输入数据:

const input = [
{
item_id: 'some_id',
map_id: 'other_id'
}
...
];

然后你可以使用 pg-promise 生成values,如下所示(参见 helpers.values(:

const values = pgp.helpers.values(input, ['item_id', 'map_id']);

或者,如果需要更多详细信息(如类型转换等(,可以将列作为完整的 ColumnSet 传递。

Postgres支持元组相等,所以你可以这样写:

DELETE FROM table
WHERE
_id = $<id>
AND (item_id, map_id) IN ( 
($<item_id>, $<map_id>),
($<another_id>, $<another_map_id>),
...
)

最新更新