简体   繁体   中英

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.

author | year | conference | paperkey
                               1234

And I have another table called base as follows:

author | year | conference | paperkey
Andrew   1999     KDD           1234

I want to insert the value retrieved in table base to table 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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