3

I'm using mysqljs\mysql in a Node application and I've run into some trouble performing statements with 'in' clauses.

Under paramater escaping, the documentation states that:

Arrays are turned into list, e.g. ['a', 'b'] turns into 'a', 'b'

Escaping query values

But when i try something like this:

var sql = "SELECT * FROM table WHERE name IN (?)";
var params = ['a', 'b', 'c'];
console.log(mysql.format(sql, params));

I get a query containing only the first value of the array:

"SELECT * FROM table WHERE name IN ('a')"
1
  • If the doc isn't correct (never used it), an alternative would be to join the array, params.join(','). But this should be done only if this is not possible with mysqljs Commented Aug 1, 2016 at 11:04

2 Answers 2

3

Try this :

var params = [['a', 'b', 'c']];
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Thanks Ebrahim. I'll accept when it's allowed in a couple of minutes.
@Stromgren, reading the SqlString code, I find that the function await an Array to put each cell on every ?, the doc say that if a cell of this array is an array, it will be converted like you want. This is why this response is working. Just for info ;)
0

If nothing else works, you could also try to write out the list items individually when formulating the statement (this should be automated). The resulting prepared statement would then look like:

"SELECT * FROM table WHERE name IN (?, ?, ?)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.