Mysql增删改查之更新记录 登录

Mysql增删改查之更新记录

更新记录

更新数据我们已经说过。需要修改内容,修改银行卡余额,修改装备信息的时候都需要使用到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;

下一节
<?php echo "Hello Mysql"; ?>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送