Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • 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: 1642
6
Android 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: 1642
6
Android 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: 21581
33
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic