• mysql 单表更新记录UPDATE


    1、单表更新

    (1)mysql> SELECT * FROM users;
    +----+----------+----------+-----+------+
    | id   | username | password | age | sex    |
    +----+----------+----------+-----+------+
    | 1   | Tom         | 123         | 25   | 1       |
    | 2   | John         | 456         | 25   | 1       |
    | 3   | Tom         | 123         | 25   | 1       |
    | 4   | John         | 456         | 16   | 1       |
    | 5   | John         | 456         | 10   | 1       |
    | 10 | John         | 456         | 16   | 1       |
    | 11 | ROSE        | 67          | 10   | 0        |
    | 12 | Ben           | 456        | 10   | NULL  |
    +----+----------+----------+-----+------+

    mysql> UPDATE users SET age = age +5;  #更新一列

    +----+----------+----------+-----+------+
    | id   | username | password | age | sex    |
    +----+----------+----------+-----+------+
    | 1   | Tom         | 123         | 30   | 1       |
    | 2   | John         | 456         | 30   | 1       |
    | 3   | Tom         | 123         | 30   | 1       |
    | 4   | John         | 456         | 21   | 1       |
    | 5   | John         | 456         | 15   | 1       |
    | 10 | John         | 456         | 21   | 1       |
    | 11 | ROSE        | 67          | 15   | 0        |
    | 12 | Ben           | 456        | 15   | NULL  |
    +----+----------+----------+-----+------+

    (2)更新多列

    mysql> UPDATE users SET age = age -id, sex = 0;

    mysql> SELECT * FROM users;
    +----+----------+----------+-----+------+
    | id | username | password | age | sex |
    +----+----------+----------+-----+------+
    | 1 | Tom | 123 | 29 | 0 |
    | 2 | John | 456 | 28 | 0 |
    | 3 | Tom | 123 | 27 | 0 |
    | 4 | John | 456 | 17 | 0 |
    | 5 | John | 456 | 10 | 0 |
    | 10 | John | 456 | 11 | 0 |
    | 11 | ROSE | 67 | 4 | 0 |
    | 12 | Ben | 456 | 3 | 0 |
    +----+----------+----------+-----+------+

    (3)加限制条件
    mysql> UPDATE users SET age = age +10 WHERE id % 2 = 0;

    +----+----------+----------+-----+------+
    | id | username | password | age | sex |
    +----+----------+----------+-----+------+
    | 1 | Tom | 123 | 29 | 0 |
    | 2 | John | 456 | 38 | 0 |
    | 3 | Tom | 123 | 27 | 0 |
    | 4 | John | 456 | 27 | 0 |
    | 5 | John | 456 | 10 | 0 |
    | 10 | John | 456 | 21 | 0 |
    | 11 | ROSE | 67 | 4 | 0 |
    | 12 | Ben | 456 | 13 | 0 |
    +----+----------+----------+-----+------+

  • 相关阅读:
    使用 linux kernel +busybox 定制linux系统
    记一次golang的内存泄露
    关于Queries_per_sec 性能计数器
    NUMA导致的MySQL服务器SWAP问题分析
    Drop Table对MySQL的性能影响分析
    当MySQL数据库遇到Syn Flooding
    tcp_tw_recycle参数引发的数据库连接异常
    一例数据同步异常问题分析
    MySQL大量线程处于Opening tables的问题分析
    MySQL DeadLock故障排查过程
  • 原文地址:https://www.cnblogs.com/toudoubao/p/6618186.html
Copyright © 2020-2023  润新知