简体   繁体   English

MySQL内部联接以更新现有表

[英]MySQL inner join to update an existing table

I have a table called empty as follows which only has the paperkey value and everything else is empty. 我有一个名为empty的表,如下所示,该表仅具有paperkey值,其他所有内容均为空。

author | year | conference | paperkey
                               1234

And I have another table called base as follows: 我还有另一个称为base表,如下所示:

author | year | conference | paperkey
Andrew   1999     KDD           1234

I want to insert the value retrieved in table base to table empty . 我想将在表base检索到的值插入表empty

I tried to query as follows but it did not work. 我尝试查询如下,但是没有用。 How can I modify this query to get the right result? 如何修改此查询以获得正确的结果?

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;

I think you are looking to update empty based on paperkey 我认为您正在寻找基于paperkey empty更新

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

You want to update existing records, not insert new ones. 您想更新现有记录,而不是插入新记录。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM