pg-promise快速删除一组元组的方法。

问题描述 投票:1回答:2

有没有更好的方法来写下面的内容。

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?

javascript sql postgresql sql-delete pg-promise
2个回答
2
投票
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']);

或者你可以通过列作为一个完整的 栏目组如果需要更多的细节,比如类型转换等等。


2
投票

Postgres支持元组平等,所以你可以把它写成。

DELETE FROM table
WHERE
    _id = $<id>
    AND (item_id, map_id) IN ( 
        ($<item_id>, $<map_id>),
        ($<another_id>, $<another_map_id>),
        ...
    )
© www.soinside.com 2019 - 2024. All rights reserved.