对于表里的记录值,可以通过update 命令进行更改,语法如下:

UPDATE tablename SET field1=value1,field2.=value2,……fieldn=valuen [WHERE CONDITION]

例如,将表emp 中ename 为“lisa”的薪水(sal)从3000 更改为4000:

mysql> update emp set sal=4000 where ename='lisa';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

在MySQL 中,update 命令可以同时更新多个表中数据,语法如下:

UPDATE t1,t2…tn set t1.field1=expr1,tn.fieldn=exprn [WHERE CONDITION]

在下例中,同时更新表emp 中的字段sal 和表dept 中的字段deptname:

mysql> select * from emp;
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| zzx | 2000-01-01 | 100.00 | 1 |
| lisa | 2003-02-01 | 200.00 | 2 |
| bjguan | 2004-04-02 | 100.00 | 1 |
| dony | 2005-02-05 | 2000.00 | 4 |
+--------+------------+---------+--------+
4 rows in set (0.00 sec)
mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | tech |
| 2 | sale |
| 5 | fin |
+--------+----------+
3 rows in set (0.00 sec)
mysql> update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where
a.deptno=b.deptno;
Query OK, 3 rows affected (0.04 sec)
Rows matched: 5 Changed: 3 Warnings: 0
mysql> select * from emp;
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| zzx | 2000-01-01 | 100.00 | 1 |
| lisa | 2003-02-01 | 400.00 | 2 |
| bjguan | 2004-04-02 | 100.00 | 1 |
| dony | 2005-02-05 | 2000.00 | 4 |
+--------+------------+---------+--------+
4 rows in set (0.01 sec)
mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | zzx |
| 2 | lisa |
| 5 | fin |
+--------+----------+
3 rows in set (0.00 sec)
自此,两个表的数据同时进行了更新。
注意:多表更新的语法更多地用在了根据一个表的字段,来动态的更新另外一个表的字段

mysql 更新数据表的记录的更多相关文章

  1. MySQL查询数据表中数据记录(包括多表查询)

    MySQL查询数据表中数据记录(包括多表查询) 在MySQL中创建数据库的目的是为了使用其中的数据. 使用select查询语句可以从数据库中把数据查询出来. select语句的语法格式如下: sele ...

  2. MYSQL查看数据表最后更新时间

    MYSQL查看数据表最后更新时间 - 拨云见日 - CSDN博客 https://blog.csdn.net/warnerwu/article/details/73352774 mysql> S ...

  3. MySQL为数据表的指定字段插入数据

    username not null 没有默认值/有默认值   insert不插入username字段 均不报错 2014年07月23日21:05    百科369 MySQL为数据表的指定字段插入数据 ...

  4. C# DataAdapter.Update() 无法更新数据表中删除的数据行

    用DataAdapter.Update() 方法更新删除了部分DataRow 的 DataTable .但是数据库中的数据没有随着更新而变化. 原因:DataTable 删除 DataRow 时,使用 ...

  5. MySQL对数据表进行分组查询

    MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...

  6. MySQL对数据表进行分组查询(GROUP BY)

    MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...

  7. MySQL 库、表、记录、相关操作(3)

    MySQL 库.表.记录.相关操作(3) 单表查询 """ 增: insert [into] [数据库名.]表名[(字段1[, ..., 字段n])] values (数 ...

  8. mysql中大数据表alter增加字段报错:"1034 Incorrect key file for table 'table_name'; try to repair it"

    mysql中大数据表alter增加字段报错:"1034 Incorrect key file for table 'table_name'; try to repair it" 现 ...

  9. MySQL中大数据表增加字段,增加索引实现

    MySQL中大数据表增加字段,通过增加索引实现 普通的添加字段sql ALTER TABLE `table_name` ADD COLUMN `num` int(10) NOT NULL DEFAUL ...

随机推荐

  1. 查看centos当前版本

    # rpm -q centos-release centos-release-5-4.el5.centos.1

  2. [Algorithm] Find first missing positive integer

    Given an array of integers, find the first missing positive integer in linear time and constant spac ...

  3. Office安装错误1402的解决

    Office软件是我们工作是必备的,为了统一公司的办工软件,要把所有的WPS和Office2003版本全部换顾Office2010.在Win7下安装Office2010一般都不会存在什么大的问题,但遇 ...

  4. 微信小程序 - 自定义swiper(dot)指示点

    点击下载示例:自定义swiper(dot)指示点

  5. head 命令(转)

    原文:http://www.cnblogs.com/peida/archive/2012/11/06/2756278.html head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结 ...

  6. Java从零开始学十六(多态)

    一.什么是多态 多态性是指允许不同类的对象对同一消息作出响应.多态性包括参数化多态性和包含多态性.多态性语言具有灵活.抽象.行为共享.代码共享的优势,很好的解决了应用程序函数同名问题.多态有两种表现形 ...

  7. Docker 安装使用

    1:安装 在Linux上安装Docker,要求64位操作系统,并且内核版本需求为3.10以上,查看本机Linux内核版本: [root@localhost ~]# uname -r -.el7.x86 ...

  8. Throwing cards away I uva1594

     Throwing cards away I Given is an ordered deck of  n  cards numbered 1 to n  with card 1 at the t ...

  9. 文本框只支持数字、小数点、退格符、负号、Del键

    Public Function OnlyNumberAndDot(inKeyAscii As Integer) As Integer '函数说明:文本框只支持数字.小数点.退格符.负号.Del键 '入 ...

  10. mysqladmin: connect to server at 'localhost' failed

    1:mysqladmin: connect to server at 'localhost' failed 2: 3: 4: