-1

I have an array of some strings in my Java code. I want to check if (and which) of the values in that array are present in the mySql database that I am using. The way I have tried to do it is query the data base for each individual value in the array. I wanted to know if there is a more efficient way of doing this? Thanks in advance.

In my java code:

String[] arrProducts=new String[]{"AB","BC","CD","AE","fg","BV","etc"};

In mySql Products database i have a productsInventory table, which has a column productId. So basically I want to check if the entire arrProduct values are present in the column productId instead of querying individually like: Select * from ProductsInventory where productId like 'AB'.

EDIT1: So my table looks like this:

ProductsInventory->{ProductName,productId}

right mow I am querying the table using one query for each value in my array.

Eg: Select * from ProductsInventory where productId like 'AB';

Select * from ProductsInventory where productId like 'BC';
Select * from ProductsInventory where productId like 'CD';

SO depending on the number of elements in my array I need to send multiple queries.

EDIT 2: And my array can change depending on user interaction. What the user enters is stored in my array and I need to check if those values are present in the database table.

4

3 回答 3

0

You can use the following with the 'in' keyword.Just pass the array as a value inside 'in' parameter the resulting query will look like this below:

select * from ProductsInventory where productId in ("AB","BC","CD","AE","fg","BV","etc");

but in will have considerable performance degradation if the array is too long.

于 2013-11-15T07:37:33.240 回答
0

As an alternative to the IN structure, or the support for regular expressions with rlike, you can use full text search. The integrated solution may be limited in speed in functionality, but 3rd party solutions (which integrate nicely with MySQL and Java) can provide nice performance for complex functionalities, like Sphinx or Solr at the cost of more resources: Full text options in MySQL that allows search in natural language.

于 2013-11-15T07:48:03.343 回答
-1

you can try something like:

 SELECT * from ProductsInventory where productId in ('AB,'BC','CD')

it will probably save you time, but dependent on whats in the database and how things are structured it could be quite expensive(time,processing)

于 2013-11-15T07:37:12.780 回答