SQL IF与ADD相同

时间:2015-09-11 14:35:34

标签: mysql if-statement add

我要做的是,在Table1.xxx中找到一个特定的字符串,如果它匹配则接受nnn和mmm中的数字并将其添加到'Table2'中的匹配uid然后删除该记录。

Table1
|   uid |   xxx |   nnn |   mmm |
---------------------------------
|   100 |   abc |   111 |   222 |
|   121 |   def |   144 |   244 |
|   100 |   ghi |   411 |   844 |

表2

|   uid |   nnn |   mmm |
 -----------------------
|   100 |   644 |   441 |

1 个答案:

答案 0 :(得分:0)

UPDATE JOIN

UPDATE table2 AS t2
JOIN table1 AS t1 ON t1.uid = t2.uid
SET t2.nnn = t2.nnn + t1.nnn, t2.mmm = t2.mmm + t1.mmm

删除表1中的记录:

DELETE t1 FROM table1 AS t1
JOIN table2 AS t2 ON t1.uid = t2.uid

您应该在事务中执行这些操作,以确保DELETE查询仅删除先前UPDATE中使用的行。