Reputation: 1895
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?
Upvotes: 13
Views: 14470
Reputation: 649
You can pass the Info.Gender
as array too:
var myQuery = `SELECT uid FROM users where Gender IN (?)`
var arrayData = ['Male', 'Female']
connection.query(myQuery, [arrayData])
Upvotes: 0
Reputation: 203329
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 ], ...);
Upvotes: 27
Reputation: 4760
You need single quotes in your query:
var myQuery = "SELECT uid FROM " +tableName+ " where Gender IN ('" + Info.Gender.join("','") + "')";
Upvotes: 2