Insert non-duplicate rows from one table to anothe

2019-08-12 06:58发布

问题:

I have two tables T1 and T2. I want to merge only those rows of T2 which is currently not there in T1 .

`Insert into T1 select * from (select * from T2 where id not in (select id from T1))`

Is there a better and faste way of achieving the same. ID column is unique across the table

回答1:

Insert into T1 
select * from T2
where id not in (select id from T1)

You could also join but then you'd need another subselect since MySQL does not want to select from a table it inserts at the same time without using a subselect.



标签: mysql insert