使用WHERE子句的具有多个表的MySQL UPDATE语法 [英] MySQL UPDATE syntax with multiple tables using WHERE clause
本文介绍了使用WHERE子句的具有多个表的MySQL UPDATE语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用id
相等的table2
中的数据更新table1
?
How to update table1
with data from table2
where id
is equal?
当我运行以下更新语句时,它将更新table1
中的所有记录(即使在table2
中不存在table1
中的id
字段的地方).
When I run the following update statement, it updates all the records in table1
(even where the id
field in table1
does not exist in table2
).
如何使用多重更新表语法来仅更新table1
中等于table2
且等于table2
的table1
中的记录?
How can I use the the multiple update table syntax, to update ONLY the records in table1
ONLY where the id
is present in table2
and equal?
UPDATE table1,table2
SET table1.value=table2.value
WHERE table2.id=table1.id
提前谢谢.
推荐答案
这是UPDATE
在MySQL
UPDATE table1 a
INNER JOIN table2 b
ON a.ID = b.ID
SET a.value = b.value
- SQLFiddle演示
- SQLFiddle Demo
这篇关于使用WHERE子句的具有多个表的MySQL UPDATE语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文