mysql互換表中兩列資料方法

2022-06-08 03:12:08 字數 4113 閱讀 1512

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)

新手可能會使用以下方法進行互換

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: 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;

執行結果:

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)

mysql互換表中兩列資料方法

小引 大千世界無奇不有,不怕做不到就怕想不到。在mysql軟體開發過程中,有時就需要把乙個表中的兩列資料進行交換。例如,我最近遇到的乙個案例是專案資料準備時客戶把 中兩列資料弄反了 把a列資料輸入到了b列,把b列資料輸入到了a列 真讓人苦笑不得。解決的最好辦法自然是直接修改系統資料庫對應表中資料 如...

mysql互換表中兩列資料方法

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...

mysql互換表中兩列資料方法

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...