View Single Post

   
  #7 (permalink)  
Old 02-28-2008, 11:29 AM
Rik Wasmus
 
Posts: n/a
Default Re: how to compare array of String with column of a table

On Fri, 26 Oct 2007 16:48:34 +0200, kath <nitte.sudhir@gmail.com> wrote:

>>> Load the array into a temporary table and use a MySQL intersect

> I would not consider this is better idea because, there creating table
> inserting values will take lot of SQL queries(statements).
> - CREATE statement = 1
> - INSERT statement = depending on the size of array


No, 1 INSERT statement would do it.

> - DELETE statement = 1


A lot of SQL queries (actually this is quite modest amount) doesn't
necessarily mean it takes longer.

>>> 0: set up table:
>>> Give the field a UNIQUE index
>>> 1: delete:
>>> DELETE FROM tablename WHERE fieldname NOT IN ('list','of','values');
>>> 2: insert:
>>> INSERT IGNORE INTO tablename (fieldname) VALUES
>>> ('list'),('of'),('values');
>>> (allthough INSERT ... ON DUPLICATE KEY UPDATE ... could be more suited
>>> to
>>> your needs)

>
> Looks good. But i have doubt again, i am using JAVA to do this task. I
> don't know exactly whether i can use String[] (array of String in
> JAVA) in the DELETE query you have mentioned. I can construct a String
> out list of String so that i can query, is it right way?.
>
> If i get how to form a query for list of values, for above DELETE then
> I as well get idea to INSERT query.
>
> How do i form a query for list of String in JAVA?.


Personally I stay as far away from JAVA as I can, put I assume an array in
JAVA can be imploded/joined somehow to a string, which would make creating
the query string quite easy.
--
Rik Wasmus
Reply With Quote