从一个表插入非重复的行到另一个mysql [英] Insert non-duplicate rows from one table to another mysql

查看:83
本文介绍了从一个表插入非重复的行到另一个mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表T1和T2.我只想合并T2中当前不在T1中的那些行.

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))`

有没有更好,更快捷的方法来实现这一目标.表格中的ID列是唯一的

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

推荐答案

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

您也可以join,但是您还需要另一个子选择,因为MySQL不想在不使用子选择的情况下从它同时插入的表中进行选择.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆