DML语句--更新(update)
程序员文章站
2022-03-03 19:51:43
...
更新记录update
UPDATE tablename SET field1=value1, field2=value2, ..., fieldn=valuen [WHERE CONDITION]
- 显示表的结构
mysql> select *from student;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | karin | 3 |
| 2 | 兔兔 | 5 |
| 3 | xiaoming | 17 |
| 4 | lihua | 18 |
| 5 | lili | 12 |
| 7 | xiaohong | 19 |
| 8 | xiaoqing | 89 |
+----+----------+-----+
7 rows in set (0.00 sec)
- 将表student中name为’karin’的年龄(age)修改为22
mysql> update student set age=22 where name='karin';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
- 显示表的结构
mysql> select *from student;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | karin | 22 |
| 2 | 兔兔 | 5 |
| 3 | xiaoming | 17 |
| 4 | lihua | 18 |
| 5 | lili | 12 |
| 7 | xiaohong | 19 |
| 8 | xiaoqing | 89 |
+----+----------+-----+
7 rows in set (0.00 sec)
update 命令可以同时更新多个表中的数据
UPDATE t1, t2, ..., tn set t1.field1=expr1, t2.field2=expr2, ..., tn.fieldn=exprn [WHERE CONDITION]
- 显示表student和studys的结构
mysql> select *from student;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | karin | 22 |
| 2 | 兔兔 | 5 |
| 3 | xiaoming | 17 |
| 4 | lihua | 18 |
| 5 | lili | 12 |
| 7 | xiaohong | 19 |
| 8 | xiaoqing | 89 |
+----+----------+-----+
7 rows in set (0.00 sec)
mysql> select *from studys;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | haha | 17 |
| 2 | dada | 67 |
+----+------+-----+
2 rows in set (0.00 sec)
mysql> update student a,studys b set a.age=a.age*b.id,b.name=a.name where a.id =b.id;
Query OK, 3 rows affected (0.03 sec)
Rows matched: 4 Changed: 3 Warnings: 0
mysql> select *from student;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | karin | 22 |
| 2 | 兔兔 | 10 |
| 3 | xiaoming | 17 |
| 4 | lihua | 18 |
| 5 | lili | 12 |
| 7 | xiaohong | 19 |
| 8 | xiaoqing | 89 |
+----+----------+-----+
7 rows in set (0.00 sec)
mysql> select *from studys;
+----+--------+-----+
| id | name | age |
+----+--------+-----+
| 1 | karin | 17 |
| 2 | 兔兔 | 67 |
+----+--------+-----+
2 rows in set (0.00 sec)
上一篇: 更新多个字段的UPDATE语句
下一篇: 数据集预处理 划分测试数据集合
推荐阅读
-
windows update 当前无法检查更新,因为未运行服务的解决方法
-
PHP新手用的Insert和Update语句构造类
-
SQLServer 2008中SQL增强之三 Merge(在一条语句中使用Insert,Update,Delete)
-
sqlserver update join 多关联更新
-
免费必须升 Win10 Anniversary Update一周年更新版太强大
-
SQL Server UPDATE语句的用法详解
-
Wind10 Update Switch软件彻底干掉自动更新 仅1MB
-
基于更新SQL语句理解MySQL锁定详解
-
Win7配置失败还原更新怎么回事 Win7配置update失败的解决办法
-
SQLServer之创建DML AFTER UPDATE触发器