I have a static method getResults(insertIds) inside a class:
static async getResults(insertIds) {
const [result] = await Driver.pool.execute(
"SELECT * FROM test WHERE id in (?)",
[insertIds]
);
return result;
}
Driver.pool returns a pool instance that was created with mysql.createPool. I'm using mysql2 library for Node.
When I log the result of MyClass.getResults([6, 8, 10, 12].join(',')), I get an array with a single result (If I don't join the ids with a comma, I get 0 rows). If I run the same query using Query window in the MySQL Workbench, I get all 4 rows back:
SELECT * FROM test WHERE id in (6, 8, 10, 12);
What am I doing wrong? Using Windows.
Edit: this SO link does not answer my question. My issue is different, I pass the array like in the provided link, but it returns only a single element.
inoperator, especially if the query like that works in general. I'd have to count the length of an array and insert?s in the query string. Would that be acceptable? I know it is generally considered a bad practice to insert string in the queries directly.?for the in, you have to pass your parameters as[[insertIds]]. Because if you only pass a flat array, each value in the array replaces one?. But if one of values in the parameter array is an array itself, the library will expand it accordingly, so that?will be replaced by the expanded array[6, 8, 10, 12].join(','), because the numbers were converted to be string as well.[[insertIds]]to the execute method, it returns an empty array.