12.8.4 更新记录
更新记录
更新数据我们已经说过。需要修改内容,修改银行卡余额,修改装备信息的时候都需要使用到 update,修改语句。
修改(也叫更新) 语句的基本语语法如下:
类别 | 详细解示 |
---|---|
基本语法 | update 表名 set 字段 1=值 1,字段 2=值 2,字段 n=值 n where 条件 |
示例 | update money set balance=balance-500 where userid = 15; |
示例说明 | 修改 money 表,将 balance 余额减 500。要求 userid 为 15 |
假设我们有下面这一个表,表结构如下:
userid | username | balance |
---|---|---|
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: 0mysql> 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: 0mysql> 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论