根据条件用另一个表的列更新列值 [英] update columns values with column of another table based on condition

查看:45
本文介绍了根据条件用另一个表的列更新列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子...

table1 ( id, item, price ) 值:

table1 ( id, item, price ) values:

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

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

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

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.

我该怎么做?

推荐答案

应该这样做:

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

你也可以试试这个:

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

这篇关于根据条件用另一个表的列更新列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆