要设置条件并替换行,请使用MySQL CASE语句。让我们首先创建一个表-
mysql> create table DemoTable1481 -> ( -> PlayerScore int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1481 values(454); mysql> insert into DemoTable1481 values(765); mysql> insert into DemoTable1481 values(890);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1481;
这将产生以下输出-
+-------------+ | PlayerScore | +-------------+ | 454 | | 765 | | 890 | +-------------+ 3 rows in set (0.00 sec)
以下是替换MySQL表中的行的查询-
mysql> update DemoTable1481 -> set PlayerScore= case when PlayerScore=454 then 1256 -> when PlayerScore=765 then 1865 -> when PlayerScore=890 then 3990 -> end -> ; Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录-
mysql> select * from DemoTable1481;
这将产生以下输出-
+-------------+ | PlayerScore | +-------------+ | 1256 | | 1865 | | 3990 | +-------------+ 3 rows in set (0.00 sec)