新手可能会使用以下方法进行互换
update product set original_price=price,price=original_price; 1 1但这样执行的结果只会使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) 123456789101112131415161718192021222324252627 123456789101112131415161718192021222324252627正确的互换方法如下:
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执行结果:
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) 123456789101112131415161718192021222324252627 123456789101112131415161718192021222324252627转载于:https://www.cnblogs.com/jym-sunshine/p/5483314.html
相关资源:DirectX修复工具V4.0增强版