返回介绍

12.8.4 更新记录

发布于 2025-01-30 20:51:03 字数 2527 浏览 0 评论 0 收藏 0

更新记录

更新数据我们已经说过。需要修改内容,修改银行卡余额,修改装备信息的时候都需要使用到 update,修改语句。

修改(也叫更新) 语句的基本语语法如下:

类别详细解示
基本语法update 表名 set 字段 1=值 1,字段 2=值 2,字段 n=值 n where 条件
示例update money set balance=balance-500 where userid = 15;
示例说明修改 money 表,将 balance 余额减 500。要求 userid 为 15

假设我们有下面这一个表,表结构如下:

useridusernamebalance
1李文凯50000.00
2黄晓明150000000.00
15马云15000.00
16陈赫1234131.00

mysql> select * from emp where deptno=15;
+------+----------+----------+
| userid |username| balance |
+------+----------+----------+
| 15 | 马云 | 15000.00 |
+------+-------+-------------+
1 row in set (0.00 sec)

使用 update 语句进行记录更新

mysql> update money set balance=balance-500 where userid = 15;
Query OK, 1 row affected (0.35 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from emp where deptno=15;
+------+----------+----------+
| userid |username| balance |
+------+----------+----------+
| 15 | 马云 | 14500.00 |
+------+-------+-------------+
1 row in set (0.00 sec)

修改多个字段

mysql> update money set balance=balance-500,username='李文凯' where userid = 15;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from emp where deptno=15;
+------+----------+----------+
| userid |username| balance |
+------+----------+----------+
| 15 |李文凯 | 14500.00 |
+------+-------+-------------+
1 row in set (0.00 sec)

同时对两个表进行更新

类别详细解示
基本语法update 表 1,表 2 set 字段 1=值 1,字段 2=值 2,字段 n=值 n where 条件
示例update money m,user u m.balance=m.balance*u.age where m.userid=u.id;
示例说明修改 money,将 money 表的别名设置为 m;user 表的别名设置为 u;将 m 表的余额改为 m 表的 balance*用户表的 age。执行条件是:m.userid = u.id

mysql> update money m,user u m.balance=m.balance*u.age where m.userid=u.id;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文