mysql列调换位置_mysql互换表中两列数据方法
1.創(chuàng)建表及記錄用于測試
CREATE TABLE `product` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '產(chǎn)品id', `name` varchar(50) NOT NULL COMMENT '產(chǎn)品名稱', `original_price` decimal(5,2) unsigned NOT NULL COMMENT '原價', `price` decimal(5,2) unsigned NOT NULL COMMENT '現(xiàn)價', 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');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
2.互換original_price與price的值
新手可能會使用以下方法進行互換
update product set original_price=price,price=original_price;
1
1
但這樣執(zhí)行的結(jié)果只會使original_price與price的值都是price的值,因為update有順序的,
先執(zhí)行original_price=price?, original_price的值已經(jīng)更新為price,
然后執(zhí)行price=original_price,這里相當于沒有更新。
執(zhí)行結(jié)果:
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
正確的互換方法如下:
update product as a, product as b set a.original_price=b.price, a.price=b.original_price where a.id=b.id;
1
1
執(zhí)行結(jié)果:
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的mysql列调换位置_mysql互换表中两列数据方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一道解决的非常漂亮的算法题
- 下一篇: 小米、OPPO、vivo齐发声:32位A