如何将记录从表插入到另一个而不重复 [英] How to insert records from table to another without duplicate

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

问题描述

我有两个表t1和t2. t1的值重复.我需要将所有记录从t1插入到t2,但是我不希望在t2中出现重复记录.我尝试了以下命令,在我看来,它似乎是正确的,运行时没有语法错误,但是问题是,它的效果为0.没有将t1中的记录插入到t2中.

I have two tables t1 and t2. t1 has duplicated values. I need to insert all records from t1 to t2, but I don't want duplicates to occur in t2. I tried the following command which seems to me correct and no syntax error when I run it but the problem, it has 0 effect. No records from t1 inserted in t2.

insert into test.t2 (name2) 
select name1 from test.t1 where NOT EXISTS (select name2 from test.t2);

有人可以帮忙吗?

推荐答案

insert into test.t2(name2) 
select distinct name1 from test.t1 where name1 NOT IN(select name2 from test.t2);

OR

insert into test.t2(name2) 
select distinct name1 from test.t1 t1 where NOT EXISTS(select name2 from test.t2 t2 where t1.name1=t2.name2);

这篇关于如何将记录从表插入到另一个而不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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