• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass a number List to SELECT ... WHERE ... IN (...)

 
Elizabeth King
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using MySQL.

To query the 'users' table with WHERE and IN with a static list, I have something like:

SELECT * FROM users WHERE user_id IN (1001, 1002, 1003, 1004); -- user_id is the PK of users table

How do I pass a dynamic list of user_id to WHERE user_id IN (....) ?

The list is an ArrayList with a dynamic length.

 
Swastik Dey
Rancher
Posts: 1721
8
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PreparedStatement might be an option i think.



 
Elizabeth King
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:PreparedStatement might be an option i think.





The list is an ArrayList with a dynamic length as well.
 
Swastik Dey
Rancher
Posts: 1721
8
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you could form the query based on the size of array list.
 
Paul Clapham
Sheriff
Posts: 21966
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First create a PreparedStatement with the right number of question marks, and then fill in all those question marks from your list.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!