0

Sadly, I somehow was unable to find an answer for this for the past two hours... I am trying to pass an array of integers into an SQL IN query like so:

"SELECT id, bio, image_url, userhandle FROM users IN ([4,6,7,8])"

Of course this is not gonna work so i tried this, but it only works with string arrays. formattedUserIds is the array of integers.

"SELECT id, bio, image_url, userhandle FROM users IN ('" + formattedUserIds.join(",") + "')"

to clarify, basically what i need is to convert [4,6,7,8] -> 4,6,7,8 so i can pass it into the in clause like IN(4,6,7,8) any help is appreciated.

2
  • 2
    You could pass-in a composite array parameter and INNER JOIN on it as an alternative to WHERE IN: stackoverflow.com/questions/47466801/… Commented Feb 15, 2021 at 3:03
  • I'm sorry, complete newbie at sql. How would i do this in my code? @Dai Commented Feb 15, 2021 at 3:28

5 Answers 5

2

Use the ANY construct instead and pass the array as is:

SELECT id, bio, image_url, userhandle
FROM   users
WHERE  id = ANY($user_ids);

Related:

Sign up to request clarification or add additional context in comments.

Comments

0

Working on the example provided by Dai, here is a way you can get this done. Use the IN Clause as follows

SELECT id, bio, image_url, userhandle 
  FROM users IN (select *
                   from unnest(ARRAY[4,6,7,8])
                )

demo
https://dbfiddle.uk/?rdbms=postgres_12&fiddle=27ca1628fb40540f9dcf07b8c4625e6e

Comments

0

Define a function to parse array values to SQL return string

const parseArrayForSqlQuery = (stringArray: number[]) => {
  let returnString = '';
  stringArray.forEach((value, index) => {
    if (index > 0) {
      returnString = returnString + ',';
    }
    returnString = returnString + `${value}`; 
    // add an inner quotes if the values are string
  });
  return returnString;
}

Consider the below array should be pass to SQL statement

const arrayValues = [4,6,7,8];

Use the SQL statement like so

`SELECT id, bio, image_url, userhandle FROM users where users.id IN (${parseArrayForSqlQuery(arrayValues})`

Expected output

`SELECT id, bio, image_url, userhandle FROM users where users.id IN (4,6,7,8)`

Comments

0

You can use find_in_set that can work with string array. e.g. 1,2,3,4

SELECT id, bio, image_url, userhandle
FROM   users
WHERE  find_in_set(users.id, $user_ids);

Comments

-1

I ended up just doing

  "SELECT id, bio, image_url, userhandle FROM users WHERE id IN (" + formattedUserIds.toString() + ")"

Thank you!

1 Comment

I downvoted this answer because of the risk of SQL injection (and because of the possibility that using variadic IN won't be able to take advantage of cached execution plans). What guarantees do you have that formattedUserIds.toString() will always return a syntactically correct substring? What if formattedUserIds is empty? If you're formatting strings using culture/locale-specific formatting then you might see commas as digit-grouping characters which will break your query. And so on and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.