使用来自另一个表的数据更新一个表。

时间:2021-01-22 23:06:10

Table 1:

表1:

id    name    desc
-----------------------
1     a       abc
2     b       def
3     c       adf

Table 2:

表2:

id    name    desc
-----------------------
1     x       123
2     y       345

How do I run an sql update query that can update Table 1 with Table 2's name and desc using the same id? So the end result I would get is

如何运行sql update查询,以便使用相同的id使用表2的名称和desc更新表1 ?最终结果是

Table 1:

表1:

id    name    desc
-----------------------
1     x       123
2     y       345
3     c       adf

How can this be done for:

如何做到这一点:

  • SQL Server
  • SQL Server
  • MySQL
  • MySQL
  • PostgreSQL
  • PostgreSQL
  • Oracle
  • 甲骨文

5 个解决方案

#1


68  

For MySql:

MySql:

UPDATE table1 JOIN table2 
    ON table1.id = table2.id
SET table1.name = table2.name,
    table1.`desc` = table2.`desc`

For Sql Server:

Sql服务器:

UPDATE   table1
SET table1.name = table2.name,
    table1.[desc] = table2.[desc]
FROM table1 JOIN table2 
   ON table1.id = table2.id

#2


17  

Oracle 11g R2:

Oracle 11 g R2:

create table table1 (
  id number,
  name varchar2(10),
  desc_ varchar2(10)
);

create table table2 (
  id number,
  name varchar2(10),
  desc_ varchar2(10)
);

insert into table1 values(1, 'a', 'abc');
insert into table1 values(2, 'b', 'def');
insert into table1 values(3, 'c', 'ghi');

insert into table2 values(1, 'x', '123');
insert into table2 values(2, 'y', '456');

merge into table1 t1
using (select * from table2) t2
on (t1.id = t2.id)
when matched then update set t1.name = t2.name, t1.desc_ = t2.desc_;

select * from table1;

        ID NAME       DESC_
---------- ---------- ----------
         1 x          123
         2 y          456
         3 c          ghi

See also Oracle - Update statement with inner join.

参见Oracle - Update语句与内部连接。

#3


6  

Try following code. It is working for me....

试代码。它是为我工作....

UPDATE TableOne 
SET 
field1 =(SELECT TableTwo.field1 FROM TableTwo WHERE TableOne.id=TableTwo.id),
field2 =(SELECT TableTwo.field2 FROM TableTwo WHERE TableOne.id=TableTwo.id)
WHERE TableOne.id = (SELECT  TableTwo.id 
                             FROM   TableTwo 
                             WHERE  TableOne.id = TableTwo.id) 

#4


6  

UPDATE table1
SET 
`ID` = (SELECT table2.id FROM table2 WHERE table1.`name`=table2.`name`)

#5


0  

Use the following block of query to update Table1 with Table2 based on ID:

使用下面的查询块来更新Table1,表2基于ID:

UPDATE Table1, Table2 
SET Table1.DataColumn= Table2.DataColumn
where Table1.ID= Table2.ID;

This is the easiest and fastest way to tackle this problem.

这是解决这个问题最简单、最快的方法。

#1


68  

For MySql:

MySql:

UPDATE table1 JOIN table2 
    ON table1.id = table2.id
SET table1.name = table2.name,
    table1.`desc` = table2.`desc`

For Sql Server:

Sql服务器:

UPDATE   table1
SET table1.name = table2.name,
    table1.[desc] = table2.[desc]
FROM table1 JOIN table2 
   ON table1.id = table2.id

#2


17  

Oracle 11g R2:

Oracle 11 g R2:

create table table1 (
  id number,
  name varchar2(10),
  desc_ varchar2(10)
);

create table table2 (
  id number,
  name varchar2(10),
  desc_ varchar2(10)
);

insert into table1 values(1, 'a', 'abc');
insert into table1 values(2, 'b', 'def');
insert into table1 values(3, 'c', 'ghi');

insert into table2 values(1, 'x', '123');
insert into table2 values(2, 'y', '456');

merge into table1 t1
using (select * from table2) t2
on (t1.id = t2.id)
when matched then update set t1.name = t2.name, t1.desc_ = t2.desc_;

select * from table1;

        ID NAME       DESC_
---------- ---------- ----------
         1 x          123
         2 y          456
         3 c          ghi

See also Oracle - Update statement with inner join.

参见Oracle - Update语句与内部连接。

#3


6  

Try following code. It is working for me....

试代码。它是为我工作....

UPDATE TableOne 
SET 
field1 =(SELECT TableTwo.field1 FROM TableTwo WHERE TableOne.id=TableTwo.id),
field2 =(SELECT TableTwo.field2 FROM TableTwo WHERE TableOne.id=TableTwo.id)
WHERE TableOne.id = (SELECT  TableTwo.id 
                             FROM   TableTwo 
                             WHERE  TableOne.id = TableTwo.id) 

#4


6  

UPDATE table1
SET 
`ID` = (SELECT table2.id FROM table2 WHERE table1.`name`=table2.`name`)

#5


0  

Use the following block of query to update Table1 with Table2 based on ID:

使用下面的查询块来更新Table1,表2基于ID:

UPDATE Table1, Table2 
SET Table1.DataColumn= Table2.DataColumn
where Table1.ID= Table2.ID;

This is the easiest and fastest way to tackle this problem.

这是解决这个问题最简单、最快的方法。