Joris Monten
Joris Monten

Reputation: 15

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!

Upvotes: 0

Views: 48

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269713

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 updates, but that is database-specific syntax.

Upvotes: 2

Related Questions