Search code examples
javasqleclipsesqlitemanager

insert data from table into another table if don't exist


hi so that's my problem :(i'm working in eclipse with java ) i have this table phone(id,mark,reference,OS) and i have 3 seller vend1,vend2,vend3(id,mark,reference,os,price) i want insert all data from vend1 and vend2 and vend3 into the table phone without price so i want to insert the phone if don't exist in the table phone because 2 or 3 seller can have the same phone but i want to insert just one in table phone. hope you can help.


Solution

  • You could use a series on insert-select statements:

    INSERT INTO phone
    SELECT is, mark, reference, os
    FROM   vend1
    WHERE  NOT EXISTS (SELECT *
                       FROM   phone
                       WHERE  phone.id = vend1.id)
    

    Similarly, you could create statements for the vend2 and vend3 tables.