Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two database tables t1 and t2 and i want to insert all data from a third table t3 to t1 without duplicate data already in both t1 and t2? Please help

What I have tried:

Cherch for ideal solution for this problem
Posted
Updated 28-Feb-17 23:53pm
Comments
Graeme_Grant 1-Mar-17 4:33am    
What have you tried?

1 solution

Assuming all the 3 tables have identical fields, say field1 and field2, except the primary keys if any,
INSERT INTO table1 (
SELECT * FROM table3 t3 WHERE NOT EXISTS (

(SELECT * FROM table1 t1 WHERE t1.field1=t3.field1 AND t1.field2=t3.field2) 
OR
(SELECT * FROM table2 t2 WHERE t2.field1=t3.field1 AND t2.field2=t3.field2)

))
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900