"where in" MySQL query in nodejs

I need to make a MySQL query using "WHERE IN". This is my request:

var myQuery = 'SELECT uid FROM ' +tableName+ ' where Gender IN (' + Info.Gender.join() + ')'; 

If I type Info.Gender , it will be ['Male', 'Female'], like a string. but when the request is executed, it says

 SELECT uid FROM appUsers where Gender IN (Male, Female) 

But it should be:

 SELECT uid FROM appUsers where Gender IN ('Male', 'Female') 

This means that it takes the value β€œWoman” not as a string.

Any ideas?

+5
source share
2 answers

You should use an escaping request (assuming you are using node-mysql ):

 var myQuery = 'SELECT uid FROM ?? where Gender IN (?)'; connection.query(myQuery, [ tableName, Info.Gender ], ...); 
+14
source

In the request, you need single quotes:

 var myQuery = "SELECT uid FROM " +tableName+ " where Gender IN ('" + Info.Gender.join("','") + "')"; 
+4
source

Source: https://habr.com/ru/post/1238364/


All Articles