2

I'm using the pg package for accessing my PostgreSQL database and would like to send a query like this:

const query = 'select * from myTable where myColumn in ($1)'

I tried multiple ways to use that query string but get errors for each of them

const listOfString = ['string1', 'string2']
db.query(query, listOfStrings)

or

db.query(query, listOfStrings.join(','))

adding a single quote to the texts does not work either

how can I execute this query?

5
  • what if use 'string1, string2' instead of ['string1', 'string2']? Commented Jun 11, 2018 at 21:49
  • 1
    Have you tried select 1 from myTable where myColumn = any(array)? Checkout this doc: postgresql.org/docs/current/static/functions-comparisons.html Commented Jun 11, 2018 at 21:53
  • that worked, thank you Commented Jun 12, 2018 at 7:25
  • @Dan you should post that as an answer, or OP could post it himself s.t. we know this question was answered Commented Jun 12, 2018 at 7:33
  • @RaulRene Will do. Commented Jun 12, 2018 at 15:43

1 Answer 1

1

Postgres doc for Array Comparisons.

To do array comparisons:

SELECT 1
FROM myTable
WHERE myColumn = ANY(myArray) --  “true” if any true result is obtained
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.