postgres icon indicating copy to clipboard operation
postgres copied to clipboard

How to use `in` condition in `where` clause

Open rjconrad opened this issue 6 years ago • 3 comments

I want to do this, but with a dynamic list of masterid's :

SELECT * FROM tstrings WHERE masterid IN (1, 2, 3)

Could that be added to the documentation?

Thanks!

rjconrad avatar Feb 27 '20 08:02 rjconrad

Ah right.

That'd simply be


const xs = [1, 2, 3]
sql`
  SELECT * FROM tstrings WHERE masterid IN (${ xs })
`

Good idea about having this example in the docs, I'll think of where to add it, but PR is also very much welcomed ;)

porsager avatar Feb 27 '20 08:02 porsager

Do note that the postgres protocol limits you to max 65534 parameters, so if your total number of dynamic parameters in the query exceeds that you'll get an error telling you that. (in this specific query it'll happen if your array's length is above 65534)

porsager avatar Feb 27 '20 08:02 porsager

Nah, I'll keep this open until an example is in the docs ;)

porsager avatar Feb 29 '20 10:02 porsager