根据MySQL中的条件替换记录?

要设置条件,请使用MySQL CASE语句。让我们首先创建一个-

create table DemoTable1481
   -> (
   -> PlayerScore int
   -> );

使用insert在表中插入一些记录-

insert into DemoTable1481 values(454);
insert into DemoTable1481 values(765);
insert into DemoTable1481 values(890);

使用选择显示表中的所有记录-

select * from DemoTable1481;

这将产生以下输出-

+-------------+
| PlayerScore |
+-------------+
|         454 |
|         765 |
|         890 |
+-------------+
3 rows in set (0.00 sec)

以下是替换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

让我们再次检查表记录-

select * from DemoTable1481;

这将产生以下输出-

+-------------+
| PlayerScore |
+-------------+
|        1256 |
|        1865 |
|        3990 |
+-------------+
3 rows in set (0.00 sec)