MySQL内连接更新现有表

时间:2014-12-16 08:58:48

标签: mysql join

我有一个名为empty的表,如下所示,只有paperkey值,其他一切都是空的。

author | year | conference | paperkey
                               1234

我还有一个名为base的表,如下所示:

author | year | conference | paperkey
Andrew   1999     KDD           1234

我想将表base中检索到的值插入表empty

我试着按如下方式查询但是没有用。如何修改此查询以获得正确的结果?

insert into empty (author, year, conference)
select T1.author, T1.year, T1.conference
from base as T1
inner join empty as T2 on T1.paperkey=T2.paperkey;

2 个答案:

答案 0 :(得分:1)

我认为您希望根据empty

更新paperkey
update empty e
join base b on b.paperkey = e.paperkey
set 
e.author = b.author,
e.year = b.year,
e.conference = b.conference

答案 1 :(得分:1)

您想要更新现有记录,而不是插入新记录。

update empty T2
join base T1 on T1.paperkey = T2.paperkey
set T2.author = T1.author,
    T2.year = T1.year,
    T2.conference = T1.conference