在 MySQL 中置换特定列值
我们首先创建一个表 −
mysql> create table DemoTable1504 -> ( -> Id int, -> FirstName varchar(20) -> ); Query OK, 0 rows affected (0.83 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1504 values(101,'Chris'); Query OK, 1 row affected (0.63 sec) mysql> insert into DemoTable1504 values(102,'Bob'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1504 values(103,'David'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1504 values(104,'Mike'); Query OK, 1 row affected (0.10 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1504 ;
这会产生以下输出 −
+------+-----------+ | Id | FirstName | +------+-----------+ | 101 | Chris | | 102 | Bob | | 103 | David | | 104 | Mike | +------+-----------+ 4 rows in set (0.00 sec)
以下是置换特定列值对应的查询 −
mysql> update DemoTable1504 -> set FirstName='Carol' -> where Id=103 -> and FirstName <> 'Carol'; Query OK, 1 row affected (0.13 sec) Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录−
mysql> select * from DemoTable1504;
这会产生以下输出 −
+------+-----------+ | Id | FirstName | +------+-----------+ | 101 | Chris | | 102 | Bob | | 103 | Carol | | 104 | Mike | +------+-----------+ 4 rows in set (0.00 sec)
广告