[转]MySQL-5.7 Update语句详解

原文地址:https://www.cnblogs.com/tongxiaoda/p/7908977.html

.语法

(1)单表

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

value:
    {expr | DEFAULT}assignment:col_name = value

assignment_list:assignment [, assignment] ...

(2)多表

UPDATE [LOW_PRIORITY] [IGNORE] table_references
    SET assignment_list
    [WHERE where_condition]

注意:
1)如果没有WHERE子句,则更新所有的行。
2)如果指定了ORDER BY子句,则按照被指定的顺序对行进行更新。
3)LIMIT子句用于给定一个限值,限制可以被更新的行的数目。
4)多表更新时ORDER BY和LIMIT不能被使用;

2.用法示例

mysql> select * from students;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aaa   | 1      |       2 |
|   2 | bbb   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | aaaa  | 1      |       1 |
|   5 | ddd   | 2      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

mysql> update students set sname='eee',gender='3' where sid=4;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from students;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aaa   | 1      |       2 |
|   2 | bbb   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | eee   | 3      |       1 |
|   5 | ddd   | 2      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

mysql> select * from students2;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aaa   | 1      |       2 |
|   2 | 222   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | eee   | 3      |       1 |
|   5 | 111   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

mysql> update students,students2 set students.sname=students2.sname,students.gender=students2.gender where students.sid=students2.sid;
Query OK, 2 rows affected (0.04 sec)
Rows matched: 5  Changed: 2  Warnings: 0

mysql> select * from students2;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aaa   | 1      |       2 |
|   2 | 222   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | eee   | 3      |       1 |
|   5 | 111   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

mysql> select * from students;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aaa   | 1      |       2 |
|   2 | 222   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | eee   | 3      |       1 |
|   5 | 111   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

1)单表修改是指修改指定单个表中的已经存在数据的一个或多个列的数值;set短语后面跟要修改的列和值;
2)where子句表示限定要修改表中的哪些数据,如果没有where子句则表示所有行都要修改;
3)order by子句表示update数据按照指定的顺序进行;
4)limit子句表示限定修改数据的行数;
5)多表修改是指修改table_references指定的多个表中满足条件的行数据,多表修改不允许使用order by和limit子句;

特别注意:where的使用

mysql> select * from students2;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aaa   | 1      |       2 |
|   2 | 222   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | eee   | 3      |       1 |
|   5 | 111   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

mysql> update students2 set sname='555';
Query OK, 5 rows affected (0.04 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> select * from students2;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | 555   | 1      |       2 |
|   2 | 555   | 2      |       2 |
|   3 | 555   | 3      |       2 |
|   4 | 555   | 3      |       1 |
|   5 | 555   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

3.属性介绍

(1)Low_priortiy
表示修改语句需要等待其他链接的读此表操作结束后再执行,只作用在MyISAM,MEMORY和MERGE存储引擎;

(2)Ignore
表示当修改语句碰到违反唯一性约束条件等情况时,语句不会报错回退而是报警告信息;
如果出现了重复关键字冲突,则这些行不会被更新。如果列被更新后,新值会导致数据转化错误,则这些行被更新为最接近的合法的值。

4.update其他用法

(1)limit和ignore

mysql> select * from students;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aaa   | 1      |       2 |
|   2 | 222   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | eee   | 3      |       1 |
|   5 | 111   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

mysql> update students set sname='abc' limit 2;
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from students;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | abc   | 1      |       2 |
|   2 | abc   | 2      |       2 |
|   3 | ccc   | 3      |       2 |
|   4 | eee   | 3      |       1 |
|   5 | 111   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

mysql> update students set sid=1 where sid=2;
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> update ignore students set sid=1 where sid=2;
Query OK, 0 rows affected, 1 warning (0.03 sec)
Rows matched: 1  Changed: 0  Warnings: 1

(2)表达式

col1只会比原值增加1
update t1 set col1=col1+1;

col2和col1的结果一样
update t1 set col1=col1+1,col2=col1

(3)order by
指定update数据的顺序,在某些情况下可以避免错误的发生;
如t表中的id字段是有唯一约束的,则以下第一个语句执行错误,第二个执行正常;

mysql> update students set sid=sid+1;
ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'
mysql> update students set sid=sid+1 order by sid desc;
Query OK, 5 rows affected (0.04 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> select * from students;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   2 | abc   | 1      |       2 |
|   3 | abc   | 2      |       2 |
|   4 | ccc   | 3      |       2 |
|   5 | eee   | 3      |       1 |
|   6 | 111   | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.01 sec)

这是由于语句执行时,是从第一个开始,当1+1=2时,违反了唯一约束;

(4)多表修改
表之间通过where条件进行join操作

update items,month set items.price=month.price where items.id=month.id;

(5)UPDATE 语句使用来自另一个表的信息
修改表 titles 中的 ytd_sales 列,以反映表 sales 中的最新销售记录。

UPDATE titles
    SET ytd_sales = titles.ytd_sales + sales.qty
       FROM titles, sales
          WHERE titles.title_id = sales.title_id
          AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)

(6)UPDATE 语句与 SELECT 语句中的 TOP 子句一起使用
对来自表 authors 的前十个作者的 state 列进行更新

UPDATE authors
SET state = 'ZZ' 
FROM (SELECT TOP 10 * FROM authors ORDER BY au_lname) AS t1
WHERE authors.au_id = t1.au_id
posted @ 2018-09-06 11:12  dirgo  阅读(382)  评论(0编辑  收藏  举报