Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL- How to select rows which matches any value from an array

Tags:

mysql

How to select rows from a table where its condition can match any value from an array.

something like this:

Select * from Table Where Name = Array_of_Names;

Array_of_Names is a java array.

like image 628
Kaushal Avatar asked Sep 14 '25 20:09

Kaushal


1 Answers

You can pass it using IN keyword in query with multiple items separated by comma in brackets like :

String query = "Select * from Table Where Name IN (";

for(int i =0 ;i<arrayName.length();i++){
  query = query + "'" +arrayName(i) + "'" + ",";
}

query = query.substring(0, query.length()-1);
query = query + ")";

// execute your query here

This ll pass your query like :

Select * from Table Where Name IN ('arrayvalue1','arrayvalue2','arrayvalue3');

as per length of array.

like image 200
user3145373 ツ Avatar answered Sep 16 '25 12:09

user3145373 ツ