1

Let's say I have those two tables:

table1

ID        value1    value2
1         NULL      NULL
2         NULL      NULL
3         NULL      NULL

table2

ID        value3    value4
5         100       400
6         200       500
7         300       600

I need a SQL-statement to get value3 and value4 of table2 ID 7 into value1 and value2 of table1 ID 1.
How do I go about that?

Thanks

sebastian
  • 478
  • 8
  • 22
  • possible duplicate of [UPDATE from SELECT using SQL Server](http://stackoverflow.com/questions/2334712/update-from-select-using-sql-server) – ryanyuyu Jan 20 '15 at 21:09

1 Answers1

2

If all you need is to update two fields in a single row, you can do it with subqueries, like this:

update table1
set
    value1 = (select value3 from table2 where id=7)
,   value2 = (select value4 from table2 where id=7)
where id=1

For updating more fields in related rows of two tables use an UPDATE with JOIN syntax appropriate from your RDBMS.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523