我有一个产品表,需要使用不同的 id 一次更新多行。尽管如此,我仍然不知道如何实现。
module.exports = sequelize.define('products', {
product_id: {
type: Sequelize.INTEGER(20),
},
product_quantity: {
type: Sequelize.INTEGER(225),
},
})
var cart_list = [
{ id: 1, curr_quantity: 1 },
{ id: 2, curr_quantity: 4 },
{ id: 3, curr_quantity: 5 },
{ id: 4, curr_quantity: 2 }
]
Product.update({
product_quantity
})
.then(product => {
})
最佳答案
正如我检查过您的代码一样,您有带有多个 id 和不同数量的 car_list 数组,因为我们使用循环和 promise 。
module.exports = sequelize.define('products', {
product_id: {
type: Sequelize.INTEGER(20),
},
product_quantity: {
type: Sequelize.INTEGER(225),
},
});
let promises = [];
var cart_list = [
{ id: 1, curr_quantity: 1 },
{ id: 2, curr_quantity: 4 },
{ id: 3, curr_quantity: 5 },
{ id: 4, curr_quantity: 2 }
]
cart_list.forEach(element => {
promises.push(Product.update({product_quantity : element.curr_quantity},{where:{'product_id' : element.id}}))
});
Promise.all(promises).then(resultdata=> {
console.log('Success-------->',resultdata)
}).catch(err =>{
console.log('Failed---------------->',err);
});
关于mysql - 序列化更新并扣除具有不同 id 的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57486836/