gpt4 book ai didi

javascript - 使用数组条件更新 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 06:46:24 25 4
gpt4 key购买 nike

我有一组唯一的 ID,如下所示:

const myIds = [2, 3, 4];

我也有一个像这样的数据库:

+-----+------+
| id | seen |
+-----+------+
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
+------------+

我想更新我的数据库,以便将数组中所有 id 的 seen 列设置为 1。这样我就得到了:

+-----+------+
| id | seen |
+-----+------+
| 0 | 0 |
| 1 | 0 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
+------------+

我已经尝试过这段代码,但它只更新一个条目:

    db.query('UPDATE table SET seen = 1 WHERE id = ?', myIds,
function (error, results) {
if (error) throw error;
console.log("ok");
});

还有其他办法吗?

感谢您的帮助。

最佳答案

试试这个:

db.query('UPDATE `table` SET seen = 1 WHERE id IN (?)', [myIds.join()],
function (error, results) {
if (error) throw error;
console.log("ok");
}
);

关于javascript - 使用数组条件更新 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49235203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com