您可以为此使用CASE语句。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) );
使用插入命令在表中插入记录-
mysql> insert into DemoTable(Name) values('John'); mysql> insert into DemoTable(Name) values('Carol'); mysql> insert into DemoTable(Name) values('David');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | David | +----+-------+ 3 rows in set (0.00 sec)
以下是结合MySQL UPDATE STATEMENTS的查询-
mysql> UPDATE DemoTable set Name= case when Name= 'John' then 'John Smith' when Name= 'Carol' then 'Carol Taylor' when Name= 'David' then 'David Miller' end where Name IN('John','Carol','David'); Rows matched: 3 Changed: 3 Warnings: 0
让我们显示表中的更新记录-
mysql> select * from DemoTable;
这将产生以下输出-
+----+--------------+ | Id | Name | +----+--------------+ | 1 | John Smith | | 2 | Carol Taylor | | 3 | David Miller | +----+--------------+ 3 rows in set (0.00 sec)