Skip to content
Advertisement

How to append values of an array to an SQL statement in Javascript

I would like to SELECT * FROM MY_USERS WHERE the USER_ID could be any of the values in the array [1, 2, 3, 4, 5, 6, 7, 8].

Is there a way I could do this without iterating over the array, constructing a new string segment and concatenating it to a final query statement?

Thank you all in advance.

EDIT

I cannot tell the exact number of the array elements in advance, so SELECT * FROM MY_USERS WHERE USER_ID IN (?, ?, ?, ?, ?, ? ,? ,?) will not do.

Advertisement

Answer

You can use template literals like this,In this way you wouldn’t have to bother about number of elements in array.See Array​.prototype​.join()

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement