-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
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]; |
@charmander
what would be correct way to use above |
where col = any($1)
use that when using an array as a param.
…On Fri, Aug 26, 2022 at 10:49 AM matt212 ***@***.***> wrote:
@charmander <https://github.com/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
—
Reply to this email directly, view it on GitHub
<#1452 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAMHIKKV5JSPWSDLIGF5Y3V3DRSDANCNFSM4D25EWPA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
thanks @brianc
|
perfecto! |
yup!
…On Fri, Aug 26, 2022 at 11:10 AM matt212 ***@***.***> wrote:
thanks @brianc <https://github.com/brianc>
so my final where clause looks like this
and lower(a."gender") = ANY ($1) and lower(a."first_name") = ANY($2)
cool
—
Reply to this email directly, view it on GitHub
<#1452 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAMHIPP4ACT6MDCFY6OLUTV3DT5TANCNFSM4D25EWPA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
How about NOT IN? |
For the reverse you can use
(Though watch out with indexing on that kind of thing as it'll probably be a full scan.) Good to keep in mind that:
Otherwise if there's more than one value in the list of values, the negated operators don't really make sense. For example, |
Using pg-format: import pgFormat from 'pg-format';
const query = pgFormat(`select ... where field in %L`, values[]);
const result = await pool.query(query); |
Is it possible to use a parameterized query with an
IN
operator?I'm imagining something like:
Currently I'm injecting the values for
IN
using a string template:The text was updated successfully, but these errors were encountered: