有没有更好的方法来写下面的内容。
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'
}
...
];
那么你可以生成 values
与 承诺 如下(见 helpers.value):
const values = pgp.helpers.values(input, ['item_id', 'map_id']);
或者你可以通过列作为一个完整的 栏目组如果需要更多的细节,比如类型转换等等。
Postgres支持元组平等,所以你可以把它写成。
DELETE FROM table
WHERE
_id = $<id>
AND (item_id, map_id) IN (
($<item_id>, $<map_id>),
($<another_id>, $<another_map_id>),
...
)