[英]Update mysql database with array condition
我有一系列獨特的ID,如下所示:
const myIds = [2, 3, 4];
我也有一個這樣的數據庫:
+-----+------+
| id | seen |
+-----+------+
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
+------------+
我想更新我的數據庫,以便seen
所有的ID在我的陣列列設置為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");
}
);
謝謝@cdaiga我設法使它工作:
db.query('UPDATE table SET seen = 1 WHERE id IN (?)', [myIds],
function (error, results, fields) {
if (error) throw error;
console.log("ok");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.