mysqlは一つの表と二列の値を交換します。

6514 ワード

FROM: http://bbs.csdn.net/topics/380025779
mysql> select * from test1
+------+-------+-------+
| sbid | Ename | type  |
+------+-------+-------+
| JS.1 |  1   |   2 |
| JS.2 |  2   |   1 |
| JS.3 |  3   |   1 |
| JS.4 |  4   |   2 |
| JS.5 |  1   |   2 |
| HN.3 |  3   |   1 |
| HN.4 |  8   |   1 |
+------+-------+-------+
7 rows in set (0.05 sec)

mysql> update test1 a ,test1 b
    -> set a.Ename=b.type ,a.type=b.Ename
    -> where a.sbid=b.sbid;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7  Changed: 7  Warnings: 0

mysql> select * from test1;
+------+-------+------+
| sbid | Ename | type |
+------+-------+------+
| JS.1 |   2 |  1  |
| JS.2 |   1 |  2  |
| JS.3 |   1 |  3  |
| JS.4 |   2 |  4  |
| JS.5 |   2 |  1  |
| HN.3 |   1 |  3  |
| HN.4 |   1 |  8  |
+------+-------+------+
7 rows in set (0.00 sec)

mysql>