[Help] How to use the IN operator with prepared statements?
See original GitHub issueHello š
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:
- Created a year ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Solved by changing
IN
to= ANY
as proposed on the Wiki.Side Note: Would be nice to have this kind of information on the website on a FAQ page as well.
cc @brianc
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:
=
, then useANY(...)
!=
, then useALL(...)