仅在 MySQL 中更新单个列值?


我们首先创建一个表 −

mysql> create table DemoTable770 (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Value int
);
Query OK, 0 rows affected (0.65 sec)

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

mysql> insert into DemoTable770(Value) values(10);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable770(Value) values(90);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable770(Value) values(160);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable770(Value) values(450);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable770(Value) values(560);
Query OK, 1 row affected (0.21 sec)

使用 select 语句显示表中的所有记录 -

mysql> select *from DemoTable770;

这将产生以下输出 -

+----+-------+
| Id | Value |
+----+-------+
|  1 |    10 |
|  2 |    90 |
|  3 |   160 |
|  4 |   450 |
|  5 |   560 |
+----+-------+
5 rows in set (0.00 sec)

以下查询仅更新单个列值 -

mysql> update DemoTable770
   set Value=Value+150
   where Id=4;
Query OK, 1 row affected (0.52 sec)
Rows matched: 1 Changed: 1 Warnings: 0

让我们检查视图的描述 -

mysql> select *from DemoTable770;

这将产生以下输出 -

+----+-------+
| Id | Value |
+----+-------+
|  1 |    10 |
|  2 |    90 |
|  3 |   160 |
|  4 |   600 |
|  5 |   560 |
+----+-------+
5 rows in set (0.00 sec)

更新于:03-Sep-2019

1K+ 阅读

开启您的 职业生涯

完成课程以获得认证

开始
广告