Home >Database >Mysql Tutorial >An explanation of the two-column data method in mysql exchange table

An explanation of the two-column data method in mysql exchange table

jacklove
jackloveOriginal
2018-06-09 09:40:592043browse

1. Create tables and records for testing

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. Exchange the values ​​​​of original_price and price

Novices may use the following methods to exchange

update product set original_price=price,price=original_price;

But the result of this execution will only make the values ​​​​of original_price and price both be the value of price, because update is sequential,
execute firstoriginal_price=price , the value of original_price has been updated to price,
Then execute price=original_price, which is equivalent to no update.

Execution result:

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)

The correct exchange method is as follows:

update product as a, product as b set a.original_price=b.price, a.price=b.original_price where a.id=b.id;

Execution results:

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)

This article explains the two-column data method in the mysql exchange table. For more information, please pay attention to php' Chinese website.

Related recommendations:

How to generate 0~1 random decimal method through php

About the use of mysql timestamp formatting function from_unixtime Instructions

Instructions on the use of mysql functions concat and group_concat

The above is the detailed content of An explanation of the two-column data method in mysql exchange table. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn