Node-postgres: parameterized query with an IN operator

Created on 14 Sep 2017  路  1Comment  路  Source: brianc/node-postgres

Is it possible to use a parameterized query with an IN operator?

I'm imagining something like:

const text = 'SELECT * FROM tbl WHERE col1 IN ($1) AND col2 = $2';
const values = [[1, 2, 3], 1000000];

Currently I'm injecting the values for IN using a string template:

const text = `SELECT * FROM tbl WHERE col1 IN (${[1, 2, 3].join(', ')}) AND col2 = $1`;
const values = [1000000];
question

Most helpful comment

Use an array parameter. (https://github.com/brianc/node-postgres/issues/82#issuecomment-18756081, https://github.com/brianc/node-postgres/issues/129#issuecomment-48633017, #623)

const text = 'SELECT * FROM tbl WHERE col1 = ANY ($1) AND col2 = $2';
const values = [[1, 2, 3], 1000000];

>All comments

Use an array parameter. (https://github.com/brianc/node-postgres/issues/82#issuecomment-18756081, https://github.com/brianc/node-postgres/issues/129#issuecomment-48633017, #623)

const text = 'SELECT * FROM tbl WHERE col1 = ANY ($1) AND col2 = $2';
const values = [[1, 2, 3], 1000000];
Was this page helpful?
0 / 5 - 0 ratings