简体   繁体   中英

SQL update values from table

I'm seriously struggling with some easy stuff.

Currently I have 2 tables (tmp_jmo and persons)

table tmp_jmo
----------
ID     ADRESS     PERSON_ID
115    Street 1   (null)
120    Street 2   (null)
121    Street 3   (null)

Table persons
ID    NAME      PERSON_ID
----------
115    John       14
120    Ellen      27
121    Mark       114

Now I want to update the Peson_id from tmp_jmo with the values from person_id (persons table)

In this case I've getting the error that there are to many values

Update tmp_jmo t SET person_id = persons.person_id where tmp_jmo.id = persons.id;

I've also tried to with temporary data but also failing.

I'm sorry to interrupt you with this kind of questions but it's ruining my day!

Many thanks!

In Standard SQL, you can do:

update tmp_jmo t 
    set person_id = (select p.person_id from persons p where tmp_jmo.id = p.id);

Many databases also support join or from in update s, but that is database-specific syntax.

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