Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

mysql - Node.js updating column from array using loops

I have an amount array that is created from req.body.amount. If I want to update the 'amount' column of my table using the elements from the array, can I do it using a loop like this:

amount.forEach(function (value) {
    let update = [value, req.body.name];
    let updatequery = "UPDATE food_item SET amount = ? WHERE name IN (?)";
    db.query(updatequery, update, (err, result) => {
        if (err) throw err;
    });
});

Do I also need another loop for the req.body.name array?

question from:https://stackoverflow.com/questions/65923152/node-js-updating-column-from-array-using-loops

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

yea u have to create a nested loop if you have an array like this: arr[4][4]

it will give a table like this:

A 3d Array

you can update columb as follows:

for(let i = 0; i < n; i++){
   for(let j = 0; j < n; j++){
      arr[i][j] = value;
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...