仅在 MySQL 表中更新单列并基于条件增加
我们先创建一个表格 -
mysql> create table DemoTable ( Name varchar(50), Score int ); Query OK, 0 rows affected (1.02 sec)
使用 insert 命令在表格中插入一些记录 -
mysql> insert into DemoTable values('Sam',45);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('Mike',28);
Query OK, 1 row affected (0.36 sec)
mysql> insert into DemoTable values('Carol',27);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('David',67);
Query OK, 1 row affected (0.19 sec)使用 select 语句显示表格中的所有记录 -
mysql> select *from DemoTable;
将生成以下输出 -
+-------+-------+ | Name | Score | +-------+-------+ | Sam | 45 | | Mike | 28 | | Carol | 27 | | David | 67 | +-------+-------+ 4 rows in set (0.00 sec)
以下是仅更新单列并基于条件增加的查询 -
mysql> update DemoTable set Score=Score+10 where Score < 30; Query OK, 2 rows affected (0.22 sec) Rows matched: 2 Changed: 2 Warnings: 0
让我们再次查看表格记录 -
mysql> select *from DemoTable;
将生成以下输出 -
+-------+-------+ | Name | Score | +-------+-------+ | Sam | 45 | | Mike | 38 | | Carol | 37 | | David | 67 | +-------+-------+ 4 rows in set (0.00 sec)
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP