javascriptmysqlnode.jswhere-clause

"where in" MySQL query in nodejs


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

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

If i print Info.Gender it will be ['Male', 'Female'], as a string. but when the query is done it says

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

But should be:

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

That means it takes the Female not as a string.

Any ideas?


Solution

  • You should use query escaping (provided that you're using node-mysql):

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