《MysqL教程MysqL 实现互换表中两列数据方法简单实例》要点:
本文介绍了MysqL教程MysqL 实现互换表中两列数据方法简单实例,希望对您有用。如果有疑问,可以联系我们。
MysqL应用由于最近项目,有这样一个需求,是把数据库中的两列数据互换,经过好久才搞定,这里写个简单实例,做过记录.
MysqL应用1.创建表及记录用于测试
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 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'),'甜点','25','12.5'),'玩具','55','45'),'钱包','285','195');
MysqL应用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应用2.互换original_price与price的值
MysqL应用新手可能会使用以下方法进行互换
MysqL应用update product set original_price=price,price=original_price;
MysqL应用但这样执行的结果只会使original_price与price的值都是price的值,因为update有顺序的,
MysqL应用先执行original_price=price ,original_price的值已经更新为price,
MysqL应用然后执行price=original_price,这里相当于没有更新.
MysqL应用执行结果:
MysqL应用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)
MysqL应用正确的互换方法如下:
MysqL应用update product as a,product as b set a.original_price=b.price,a.price=b.original_price where a.ID=b.ID;
MysqL应用执行结果:
MysqL应用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应用MysqL> update product as a,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应用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教程mysql 实现互换表中两列数据方法简单实例全部内容,希望文章能够帮你解决MYSQL教程mysql 实现互换表中两列数据方法简单实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)