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

80 views Asked by At

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.

2

There are 2 answers

1
Mureinik On BEST ANSWER

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.

0
JToddler On

You can use MERGE statement. You can accomplish your requirement with merge.