zoukankan      html  css  js  c++  java
  • mysql互换表中两列数据方法

    1.创建表及记录用于测试

    CREATE TABLE `product` (
     `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '产品id',
     `name` varchar(50) NOT NULL COMMENT '产品名称',
     `original_price` decimal(5,2) unsigned NOT NULL COMMENT '原价',
     `price` decimal(5,2) unsigned NOT NULL COMMENT '现价',
     PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    INSERT INTO `product` (`id`, `name`, `original_price`, `price`) VALUES 
    (NULL, '雪糕', '5', '3.5'), 
    (NULL, '鲜花', '18', '15'), 
    (NULL, '甜点', '25', '12.5'), 
    (NULL, '玩具', '55', '45'), 
    (NULL, '钱包', '285', '195');
    mysql> select * from product;
    +----+--------+----------------+--------+
    | id | name   | original_price | price  |
    +----+--------+----------------+--------+
    |  1 | 雪糕   |           5.00 |   3.50 |
    |  2 | 鲜花   |          18.00 |  15.00 |
    |  3 | 甜点   |          25.00 |  12.50 |
    |  4 | 玩具   |          55.00 |  45.00 |
    |  5 | 钱包   |         285.00 | 195.00 |
    +----+--------+----------------+--------+
    5 rows in set (0.00 sec)

    2.互换original_price与price的值

    新手可能会使用以下方法进行互换

    update product set original_price=price,price=original_price;
    • 1

    但这样执行的结果只会使original_price与price的值都是price的值,因为update有顺序的, 
    先执行original_price=price , original_price的值已经更新为price, 
    然后执行price=original_price,这里相当于没有更新。

    执行结果:

    mysql> select * from product;
    +----+--------+----------------+--------+
    | id | name   | original_price | price  |
    +----+--------+----------------+--------+
    |  1 | 雪糕   |           5.00 |   3.50 |
    |  2 | 鲜花   |          18.00 |  15.00 |
    |  3 | 甜点   |          25.00 |  12.50 |
    |  4 | 玩具   |          55.00 |  45.00 |
    |  5 | 钱包   |         285.00 | 195.00 |
    +----+--------+----------------+--------+
    5 rows in set (0.00 sec)
    
    mysql> update product set original_price=price,price=original_price;
    Query OK, 5 rows affected (0.00 sec)
    Rows matched: 5  Changed: 5  Warnings: 0
    
    mysql> select * from product;
    +----+--------+----------------+--------+
    | id | name   | original_price | price  |
    +----+--------+----------------+--------+
    |  1 | 雪糕   |           3.50 |   3.50 |
    |  2 | 鲜花   |          15.00 |  15.00 |
    |  3 | 甜点   |          12.50 |  12.50 |
    |  4 | 玩具   |          45.00 |  45.00 |
    |  5 | 钱包   |         195.00 | 195.00 |
    +----+--------+----------------+--------+
    5 rows in set (0.00 sec)



    正确的互换方法如下:

    update product as a, product as b set a.original_price=b.price, a.price=b.original_price where a.id=b.id;
    • 1

    执行结果:

    mysql> select * from product;
    +----+--------+----------------+--------+
    | id | name   | original_price | price  |
    +----+--------+----------------+--------+
    |  1 | 雪糕   |           5.00 |   3.50 |
    |  2 | 鲜花   |          18.00 |  15.00 |
    |  3 | 甜点   |          25.00 |  12.50 |
    |  4 | 玩具   |          55.00 |  45.00 |
    |  5 | 钱包   |         285.00 | 195.00 |
    +----+--------+----------------+--------+
    5 rows in set (0.00 sec)
    
    mysql> update product as a, product as b set a.original_price=b.price, a.price=b.original_price where a.id=b.id;
    Query OK, 5 rows affected (0.01 sec)
    Rows matched: 5  Changed: 5  Warnings: 0
    
    mysql> select * from product;
    +----+--------+----------------+--------+
    | id | name   | original_price | price  |
    +----+--------+----------------+--------+
    |  1 | 雪糕   |           3.50 |   5.00 |
    |  2 | 鲜花   |          15.00 |  18.00 |
    |  3 | 甜点   |          12.50 |  25.00 |
    |  4 | 玩具   |          45.00 |  55.00 |
    |  5 | 钱包   |         195.00 | 285.00 |
    +----+--------+----------------+--------+
    5 rows in set (0.00 sec)

    最后我用
    update product as a, product as b set a.original_price=b.price, a.price=b.original_price where a.id=b.id;

    http://blog.csdn.net/fdipzone/article/details/50864196

     
  • 相关阅读:
    oracle 11g如何完全卸载
    【Python】Django Model 怎么使用 UUID 作为主键?
    【云计算】K8S DaemonSet 每个node上都运行一个pod
    【Linux】为啥查某个进程的线程,查出来的所有线程的pid不一样啊
    【JavaScript】SVG vs Canvas vs WebGL
    【Python】使用 sphinx 制作简洁而又美观的文档
    【云计算】监控 告警 怎么做
    【Redis】Redis分布式集群几点说道
    【Hadoop】HIVE 小结概览
    【Hadoop】Hive HSQ 使用 && 自定义HQL函数
  • 原文地址:https://www.cnblogs.com/cxscode/p/8361433.html
Copyright © 2011-2022 走看看