使用 UPDATE
语句修改表中的字段值。
示例如下:
创建示例表
t1
。obclient> CREATE TABLE t1(c1 int primary key, c2 int);
Query OK, 0 rows affected (0.16 sec)
obclient> INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(4,4);
Query OK, 4 rows affected (0.01 sec)
obclient> SELECT * FROM t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.06 sec)
单表更新:将表
t1
中t1.c1=1
对应的那一行数据的c2
列值修改为100
。obclient> UPDATE t1 SET t1.c2 = 100 WHERE t1.c1 = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
obclient> SELECT * FROM t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 100 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.01 sec)
单表更新:直接操作子查询,将子查询中
v.c1=1
对应的那一行数据的c2
列值修改为100
。obclient> update (SELECT * FROM t1)v SET v.c2 = 100 WHERE v.c1 = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
obclient> SELECT * FROM t1;
+----+------+
| C1 | C2 |
+----+------+
| 1 | 100 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.01 sec)
更多 UPDATE
语句相关的语法请参见《SQL 参考(Oracle 模式)》中 UPDATE 章节。