根据学号调换两名学生的宿舍(需要添加事务),学号和宿舍号已在数据库表里,怎么写可以实现数据库里的宿舍号调换
UPDATE
tableName t
JOIN
tableName t1
ON
t.学号字段 = 8 AND t1.学号字段 = 11
SET
t.宿舍号 = t1.宿舍号, t1.宿舍号= t.宿舍号
互换呗,
两个学生的学号都知道了,通过两个学号查询出对应的宿舍号,然后分别update到对立学号里去
这不很简单嘛?
首先-----
mysql> delimiter //
mysql> start transaction //
Query OK, 0 rows affected (0.00 sec)
mysql> update userinfo set username='张三' where user_id=1002 ;
-> update userinfo set username='李四' where user_id=1003 ;
-> commit //
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)