旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

关于mysql交换表中两列数据要领的解说【MySQL教程】,mysql,互换

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:44评论:0


导读:1.建立表及纪录用于测试CREATETABLE`product`(`id`int(10)unsignedNOTNULLAUTO_INCREMENTCOMME...

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: 0mysql> 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交换表中两列数据要领,更多先关内容请关注php'中文网。

相干引荐:

怎样经由过程php生成0~1随机小数要领

关于mysql 时候戳格式化函数from_unixtime的运用说明

关于mysql函数concat与group_concat运用说明事项

以上就是关于mysql交换表中两列数据要领的解说的细致内容,更多请关注ki4网别的相干文章!

标签:mysql互换


欢迎 发表评论: