mysql 實現互換表中兩列資料方法簡單一實例_Mysql

來源:互聯網
上載者:User

由於最近項目,有這樣一個需求,是把資料庫中的兩列資料互換,經過好久才搞定,這裡寫個簡單一實例,做過記錄。

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;

但這樣執行的結果只會使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: 0mysql> 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;

執行結果:

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)

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.