使用 UPDATE 语句修改表中的字段值。

    示例如下:

    创建示例表 t1t2

    1. obclient> CREATE TABLE t1(c1 int primary key, c2 int);
    2. Query OK, 0 rows affected (0.16 sec)
    3. obclient> INSERT t1 VALUES(1,1),(2,2),(3,3),(4,4);
    4. Query OK, 4 rows affected (0.02 sec)
    5. Records: 4 Duplicates: 0 Warnings: 0
    6. obclient> SELECT * FROM t1;
    7. +----+------+
    8. | c1 | c2 |
    9. +----+------+
    10. | 1 | 1 |
    11. | 2 | 2 |
    12. | 3 | 3 |
    13. | 4 | 4 |
    14. +----+------+
    15. 4 rows in set (0.06 sec)
    16. obclient> CREATE TABLE t2(c1 int primary key, c2 int) partition by key(c1) partitions 4;
    17. Query OK, 0 rows affected(0.19 sec)
    18. obclient> INSERT t2 VALUES(5,5),(1,1),(2,2),(3,3);
    19. Query OK, 4 rows affected (0.01 sec)
    20. Records: 4 Duplicates: 0 Warnings: 0
    21. obclient> SELECT * FROM t2;
    22. +----+------+
    23. | c1 | c2 |
    24. +----+------+
    25. | 5 | 5 |
    26. | 1 | 1 |
    27. | 2 | 2 |
    28. | 3 | 3 |
    29. +----+------+
    30. 4 rows in set (0.02 sec)
    • 将表 t1t1.c1=1 对应的那一行数据的 c2 列值修改为 100

      1. obclient> UPDATE t1 SET t1.c2 = 100 WHERE t1.c1 = 1;
      2. Query OK, 1 row affected (0.02 sec)
      3. Rows matched: 1 Changed: 1 Warnings: 0
      4. obclient> SELECT * FROM t1;
      5. +----+------+
      6. | c1 | c2 |
      7. +----+------+
      8. | 1 | 100 |
      9. | 2 | 2 |
      10. | 3 | 3 |
      11. | 4 | 4 |
      12. +----+------+
      13. 4 rows in set (0.01 sec)
    • 将表 t1 中按照 c2 列排序的前两行数据的 c2 列值修改为 100

      1. obclient> UPDATE t1 set t1.c2 = 100 ORDER BY c2 LIMIT 2;
      2. Query OK, 2 rows affected (0.02 sec)
      3. Rows matched: 2 Changed: 2 Warnings: 0
      4. obclient> SELECT * FROM t1;
      5. +----+------+
      6. | c1 | c2 |
      7. +----+------+
      8. | 1 | 100 |
      9. | 2 | 100 |
      10. | 3 | 3 |
      11. | 4 | 4 |
      12. +----+------+
      13. 4 rows in set (0.01 sec)
    • 将表 t2p2 分区的数据中 t2.c1 > 2 的对应行数据的 c2 列值修改为 100

      1. obclient> SELECT * FROM t2 partition (p2);
      2. +----+------+
      3. | c1 | c2 |
      4. +----+------+
      5. | 1 | 1 |
      6. | 2 | 2 |
      7. | 3 | 3 |
      8. +----+------+
      9. 3 rows in set (0.01 sec)
      10. obclient> UPDATE t2 partition(p2) SET t2.c2 = 100 WHERE t2.c1 > 2;
      11. Query OK, 1 row affected (0.02 sec)
      12. Rows matched: 1 Changed: 1 Warnings: 0
      13. obclient> SELECT * FROM t2 partition (p2);
      14. +----+------+
      15. | c1 | c2 |
      16. +----+------+
      17. | 1 | 1 |
      18. | 2 | 2 |
      19. | 3 | 100 |
      20. +----+------+
      21. 3 rows in set (0.00 sec)
    • 修改多个表。对于表 t1 和表 t2 中满足 t1.c2 = t2.c2 对应行的数据,将表 t1 中的 c2 列值修改为 100,表 t2 中的 c2 列值修改为 200

      1. obclient> UPDATE t1,t2 SET t1.c2 = 100, t2.c2 = 200 WHERE t1.c2 = t2.c2;
      2. Query OK, 6 rows affected (0.03 sec)
      3. Rows matched: 6 Changed: 6 Warnings: 0
      4. obclient> SELECT * FROM t1;
      5. +----+------+
      6. | c1 | c2 |
      7. +----+------+
      8. | 1 | 100 |
      9. | 2 | 100 |
      10. | 3 | 100 |
      11. | 4 | 4 |
      12. +----+------+
      13. 4 rows in set (0.00 sec)
      14. obclient> SELECT * FROM t2;
      15. +----+------+
      16. | c1 | c2 |
      17. +----+------+
      18. | 5 | 5 |
      19. | 1 | 200 |
      20. | 2 | 200 |
      21. | 3 | 200 |
      22. +----+------+
      23. 4 rows in set (0.01 sec)

    更多 UPDATE 语句相关的语法请参见《SQL 参考(MySQL 模式)》中 UPDATE 章节。