mysql用另一个表中的值更新列

2022-01-17 00:00:00 sql-update sql mysql

我有两张桌子,看起来都像

I have two tables, both looking like

id  name  value
===================
1   Joe     22
2   Derk    30

我需要根据每个表中的检查名称将value的值从tableA复制到tableB.

I need to copy the value of value from tableA to tableB based on check name in each table.

UPDATE 语句的任何提示?

Any tips for this UPDATE statement?

推荐答案

如果你需要根据tableA.value动态改变tableB.value,你可以这样做例如:

In addition to this answer if you need to change tableB.value according to tableA.value dynamically you can do for example:

UPDATE tableB
INNER JOIN tableA ON tableB.name = tableA.name
SET tableB.value = IF(tableA.value > 0, tableA.value, tableB.value)
WHERE tableA.name = 'Joe'

相关文章