mysql如何把利用update数据到别一张表,mysql如何update时操作两张表,摸索了一个早上,不得其果,经师父指点,贴了一下我在本地上的操作过程具体的过程
mysql> select * from tablea;
+--------+-------------+-------+
| qq | fscorecount | score |
+--------+-------------+-------+
| 123456 | 20 | 100 |
| 123457 | 20 | 99 |
| 123458 | 20 | 98 |
+--------+-------------+-------+
3 rows in set (0.08 sec)
mysql> select * from tableb;
+--------+-------+
| qq | score |
+--------+-------+
| 123458 | 10 |
| 123457 | 10 |
| 123457 | 10 |
| 123456 | 10 |
+--------+-------+
4 rows in set (0.00 sec)
mysql> update tableb set score=score+(select score from tablea where tablea.qq=tableb.qq);
mysql> select * from tableb;
+--------+-------+
| qq | score |
+--------+-------+
| 123458 | 108 |
| 123457 | 109 |
| 123457 | 109 |
| 123456 | 110 |
+--------+-------+
4 rows in set (0.00 sec)
mysql> update tablea set fscorecount=fscorecount+score,score=0 where score>0
-> ;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from tablea;
+--------+-------------+-------+
| qq | fscorecount | score |
+--------+-------------+-------+
| 123456 | 120 | 0 |
| 123457 | 119 | 0 |
| 123458 | 118 | 0 |
+--------+-------------+-------+
3 rows in set (0.00 sec)
mysql>