[转]mysql 一个表两列的值交换

时间:2023-12-09 23:48:49

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>