I have to write an SQL query to find the id's in a table which are similar to the ID's of another table.
The problem while querying from the TABLE_B is that, in TABLE_B these queries will be having some String attached to it.
For example:
If the ID passed is: 123456789
Then in TABLE_B it will be like ABC12456789XYZ
So to select these, I thought of writing an SQL query as shown below, iterating thousands of and
clauses:
String idCsList = "";
int i = 1;
for( String ids : idList ) {
if( i == 1 ) {
idCsList = idCsList + "'%" + ids + "%'" + ")";
i++;
continue;
}
idCsList = idCsList + " AND TABLE_B.id LIKE (" + "'%" + ids + "%'" + ")";
i++;
}
But this idea will not work because of the limit on the length of an SQL query, and the query will fail. It also takes too long.
Is there a better way to query using many wildcard operators in a more performance optimized way?