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.
I found an answer. I should have used pool.query
instead of pool.execute
. You can read full explanation in this Mysql2 Github issue
This is the way prepared statements work, I think we should document that better
.execute() under the hood is doing prepare + execute commands