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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1746125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:31:26  来源:igfitidea点击:

update columns values with column of another table based on condition

sqlmysqlsql-serveroracle

提问by niceApp

I have two tables...

我有两张桌子...

table1 ( id, item, price ) values:

table1 ( id, item, price ) 值:

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

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

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

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

Now I want to:

现在我想:

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

How do I do it?

我该怎么做?

回答by RageZ

Something like this should do it :

像这样的事情应该这样做:

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

You can also try this:

你也可以试试这个:

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

回答by Nadeem

This will surely work:

这肯定会奏效:

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