Pass array in Mysql query with nodejs

Viewed 8198

I have a simple query that I want to pass an array inside which has 5 items. I am using the mysql module so I know it can be done but am not doing the synatx right and therefore getting a syntax error.

Below is the query:

`UPDATE table1 SET table1.col=0 WHERE (table1.col2) IN = (?) AND table1.id=(SELECT ...);`,[arr]

//arr = [1,2,3,4,5];

I have tried:

`UPDATE table1 SET table1.col=0 WHERE (table1.col2) IN = (?,?,?,?,?) AND table1.id=(SELECT ...);`,[arr]`

but I still get a syntax error.

3 Answers

In my case, array inside of array is needed to get this working. Just array variable as parameter passed only first number to sql.

Here is an example: (Notice ids inside of array as the second parameter)

var sql = "SELECT * FROM table WHERE ID IN (?)";
var ids = [1,2,3];

pool.query(sql, [ids], function (err, result, fields) {

    if(err) {
        console.log(err);
    }
    else {
        console.log(result);
    }
}
Related