Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass list of ids to mysql delete query from NodeJs

i need to do a multiple row delete query with the where clause in list (id1,id2..) But since we don't have lists in JS i'm having a problem with passing the parameters to the query.This is my code:

 let list =[1,2,..];
 let Query = 'DELETE FROM Table WHERE id IN (?)';
 connection.query(Query,list, function (err, result) {
     `enter code here`
 };

when i pass it this way and after logging the mysql server to a Logfile i see that it actually passes only the first id.

PS : i tried without the parentheses around ? and also doing a Array.join on the list but both didn't work.

like image 697
Aziz Fekih Avatar asked Mar 08 '23 16:03

Aziz Fekih


1 Answers

Read in document of https://github.com/mysqljs/mysql#performing-queries (if you use this lib to connect mysql)

 let list =[1,2,..];
 let Query = 'DELETE FROM Table WHERE id IN (?)';
 // values param need is a array of value to binding to "?"
 // you can try [list.toString()] if the block above not working
 connection.query(Query,[list], function (err, result) {
     `enter code here`
 };
like image 88
hoangdv Avatar answered Apr 27 '23 06:04

hoangdv