admin管理员组文章数量:1186421
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?
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?
Share Improve this question edited Dec 22, 2016 at 13:25 Dan The Man asked Dec 16, 2015 at 10:22 Dan The ManDan The Man 1,8957 gold badges31 silver badges52 bronze badges 2 |3 Answers
Reset to default 27You 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 ], ...);
You need single quotes in your query:
var myQuery = "SELECT uid FROM " +tableName+ " where Gender IN ('" + Info.Gender.join("','") + "')";
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])
本文标签: javascriptquotwhere inquot MySQL query in nodejsStack Overflow
版权声明:本文标题:javascript - "where in" MySQL query in nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738267980a2072132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
' where Gender IN (\'' + Info.Gender.join("', '") + \'')';
– fuyushimoya Commented Dec 16, 2015 at 10:24