如何用条件替换MySQL表中的行?
要设置条件并替换行,请使用MySQLCASE语句。让我们首先创建一个表-
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)
热门推荐
1 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练