question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[Help] How to use the IN operator with prepared statements?

See original GitHub issue

Hello šŸ‘‹

Iā€™m having a doubt on the best way to use the following query via the prepared statements:

SELECT *
  FROM table
WHERE ID IN (id1, id2, ...) 

What Iā€™m trying is:

const ids = ['01GG7E75ZBMSQQ6FPG5456ZHRP', '01GG7EY1XRJKF0KTA4GVZMST6A', '01GG7XHTVC5003RAVBENMC96CP']
const result = await this.db.query({
      name: 'get-by-ids',
      text: `SELECT * FROM table WHERE ID IN ($1)`,
      values: [ids],
})

But doesnā€™t return anything or an error. Any thoughts?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rafaell-lycancommented, Nov 4, 2022

Solved by changing IN to = ANY as proposed on the Wiki.

const ids = ['01GG7E75ZBMSQQ6FPG5456ZHRP', '01GG7EY1XRJKF0KTA4GVZMST6A', '01GG7XHTVC5003RAVBENMC96CP']
const result = await this.db.query({
      name: 'get-by-ids',
      text: `SELECT * FROM table WHERE ID = ANY ($1)`,
      values: [ids],
})

Side Note: Would be nice to have this kind of information on the website on a FAQ page as well.

cc @brianc

0reactions
sehropecommented, Nov 17, 2022

For the NOT IN case you need to use: id != ALL($1) (ā€œid is not equal to all of these valuesā€)

SQL with id != ANY($1) would not work as if thereā€™s more than one value in $1, then id will definitely not be equal to one of those values.

For example if $1 = [1,2,3, 4], then regardless of the value of ā€œidā€, it will always be != to at least one of those values as even if it matches one value, it definitely does not match the rest.

So in summary:

  • If itā€™s =, then use ANY(...)
  • If itā€™s !=, then use ALL(...)
Read more comments on GitHub >

github_iconTop Results From Across the Web

java - PreparedStatement with list of parameters in a IN clause
What I do is to add a "?" for each possible value. var stmt = String.format("select * from test where field in (%s)",...
Read more >
JDBC PreparedStatement SQL IN condition - Mkyong.com
Java JDBC PreparedStatement example to create a SQL IN condition. 1. PreparedStatement + Array. In JDBC, we can use createArrayOf to createĀ ...
Read more >
Using Prepared Statements - JDBC Basics - Oracle Help Center
This JDBC Java tutorial describes how to use JDBC API to create, insert into, update, and query tables. You will also learn how...
Read more >
Java JDBC PreparedStatement with a list of values in an IN ...
In this article, we will discuss how to add a list of values dynamically to IN clause using JDBC PreparedStatement. Consider we have...
Read more >
How to set values to list of parameters of IN clause on ...
How to set values to list of parameters of IN clause on PreparedStatement using JDBC? - The IN clause in MYSQL database is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found