Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameterized query with an IN operator #1452

Closed
thewizarodofoz opened this issue Sep 14, 2017 · 9 comments
Closed

parameterized query with an IN operator #1452

thewizarodofoz opened this issue Sep 14, 2017 · 9 comments
Labels

Comments

@thewizarodofoz
Copy link

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];
@thewizarodofoz thewizarodofoz changed the title parameterized parameterized query with an IN operator Sep 14, 2017
@charmander
Copy link
Collaborator

Use an array parameter. (#82 (comment), #129 (comment), #623)

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

@matt212
Copy link

matt212 commented Aug 26, 2022

@charmander
well below is not working

const text = 'SELECT * FROM employees WHERE lower(a."first_name") IN ($1)   AND lower(a."gender") IN ($2)';
const values = [['aamer','babette'], ['f']];

what would be correct way to use above

@brianc
Copy link
Owner

brianc commented Aug 26, 2022 via email

@matt212
Copy link

matt212 commented Aug 26, 2022

thanks @brianc
so my final where clause looks like this

and lower(a."gender") = ANY ($1) and lower(a."first_name") = ANY($2)
cool

@brianc
Copy link
Owner

brianc commented Aug 27, 2022

perfecto!

@brianc
Copy link
Owner

brianc commented Oct 11, 2022 via email

@rockie
Copy link

rockie commented May 26, 2024

How about NOT IN?

@sehrope
Copy link
Contributor

sehrope commented May 26, 2024

For the reverse you can use ALL(...) in the same manner:

SELECT *
FROM some_table t
WHERE t.id != ALL($1::int[])

(Though watch out with indexing on that kind of thing as it'll probably be a full scan.)

Good to keep in mind that:

  • With = you almost always want to use = ANY(...)
  • With != you almost always want to use != ALL(...)

Otherwise if there's more than one value in the list of values, the negated operators don't really make sense. For example, x != ANY('{1,2,3}'::int[]) will always be true because if the value matches any one item of the array, it definitely does not match the rest. Similarly, x = ALL('{1,2,3}'::int[]) will always be false because a value cannot equal more than one thing.

@evgeny-kim
Copy link

Using pg-format:

import pgFormat from 'pg-format';

const query = pgFormat(`select ... where field in %L`, values[]);
const result = await pool.query(query);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants