简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM