更新在MySql中连接两个表

时间:2014-03-29 21:49:39

标签: mysql

我在mysql db中有两个表:

1)dotable_new;

2)dotable_new_new

现在我需要更新我的表dotable_new_new中值为TOTAL的表dotable_new中的值TOTAL。

我正在尝试此更新查询但没有成功......你能帮助我吗?

UPDATE dotable_new a, dotable_new_new tmp
SET a.total = tmp.total
WHERE
    a.rdt IN ('tot mac')

dotable_new;

+---------+---------------+--------+-------+----+
| RDT     | TYPE          | NUMBER | TOTAL | ID |
+---------+---------------+--------+-------+----+
| tot mac | tot           |   3209 |  3249 |  1 |
+---------+---------------+--------+-------+----+

dotable_new_new

+---------+-------+----+
| RDT     | TOTAL | ID |
+---------+-------+----+
| tot mac | 10899 |  5 |
+---------+-------+----+

2 个答案:

答案 0 :(得分:1)

选中此项会在how to join two tables in update

上为您提供帮助

试试:

  UPDATE dotable_new a
  INNER JOIN dotable_new_new tmp ON tmp.rdt = a.rdt
  SET a.total =  tmp.total
  WHERE a.rdt = 'tot mac'

答案 1 :(得分:0)

试试这个:

UPDATE dotable_new a
SET a.total = (SELECT tmp.total
               FROM  dotable_new_new tmp
               WHERE tmp.rdt = a.rdt)

如果您只想更新记录rdt = 'tot mac',请将以下行添加到结尾:

WHERE a.rdt = 'tot mac'