根据条件使用另一个表的列更新列值

时间:2009-11-17 01:57:05

标签: sql mysql sql-server oracle

我有两张桌子......

table1(id,item,price)值:

id | item | price
-------------
10 | book | 20  
20 | copy | 30   
30 | pen  | 10

.... table2(id,item,price)值:

id | item | price
-------------
10 | book | 20
20 | book | 30

现在我想:

update table1 
   set table1.Price = table2.price 
 where table1.id = table2.id
   and table1.item = table2.item.

我该怎么做?

2 个答案:

答案 0 :(得分:174)

这样的事情应该这样做:

UPDATE table1 
   SET table1.Price = table2.price 
   FROM table1  INNER JOIN  table2 ON table1.id = table2.id

你也可以试试这个: update table1 set price =(从table2中选择table1.id = table2.id的价格);

答案 1 :(得分:75)

这肯定有用:

UPDATE table1
SET table1.price=(SELECT table2.price
  FROM table2
  WHERE table2.id=table1.id AND table2.item=table1.item);