i would like to convert a javascript array ids = [ 378, 464 ] to an array that MySQL is successfully parsing.
My SQL query includes the array like this:
Do something WHERE id IN ("472", "467").
This query works in MySQL Workbench.
I am struggling to get the array in the needed structure. I tried the following javascript structures within my api but i cannot get it to work.
("378", "464")
[ 378, 464 ]
('472', '467')
MYSQL Error Message:
code: 'ER_PARSE_ERROR',
errno: 1064,
You could stringify a stringed value and join all values with comma.
var ids = [378, 464],
formatted = `(${ids.map(v => JSON.stringify(v.toString())).join(', ')})`;
console.log(formatted);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With