Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Javascript Array to MySQL Array

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,
like image 883
anderwald Avatar asked Sep 13 '25 17:09

anderwald


1 Answers

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);
like image 126
Nina Scholz Avatar answered Sep 16 '25 06:09

Nina Scholz