
To swap two columns, we can apply the below swapping logic.
Add both values and store them into the first column
Subtract the first column’s value from the second and store it into the second column.
Subtract the first column’s value from the updated second column and store it into the first.
The above rule structure is as follows. Suppose, the first column is a and the second column is b.
1. a = a+b; 2. b = a-b; 3. a = a-b;
Now we will apply the above rule in order to swap the two column values.
Creating a table.
mysql> create table SwappingTwoColumnsValueDemo -> ( -> FirstColumnValue int, -> SecondColumnValue int -> ); Query OK, 0 rows affected (0.49 sec)
Inserting some records.
本文档主要讲述的是MATLAB与VB混合编程技术研究;着重探讨了在VB应用程序中集成MATLAB实现程序优化的四种方法,即利用Matrix VB、调用DLL动态链接库、应用Active自动化技术和动态数据交换技术,并分析了集成过程中的关键问题及其基本步骤。这种混合编程实现了VB的可视化界面与MATLAB强大的数值分析能力的结合。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看
0
mysql> insert into SwappingTwoColumnsValueDemo values(10,20),(30,40),(50,60),(70,80),(90,100); Query OK, 5 rows affected (0.19 sec) Records: 5 Duplicates: 0 Warnings: 0
To check the column values before swapping.
mysql> select *from SwappingTwoColumnsValueDemo;
The following is the output.
+------------------+-------------------+ | FirstColumnValue | SecondColumnValue | +------------------+-------------------+ | 10 | 20 | | 30 | 40 | | 50 | 60 | | 70 | 80 | | 90 | 100 | +------------------+-------------------+ 5 rows in set (0.00 sec)
Syntax to swap column values.
mysql> UPDATE SwappingTwoColumnsValueDemo -> SET FirstColumnValue = FirstColumnValue+SecondColumnValue, -> SecondColumnValue = FirstColumnValue-SecondColumnValue, -> FirstColumnValue = FirstColumnValue-SecondColumnValue; Query OK, 5 rows affected (0.15 sec) Rows matched: 5 Changed: 5 Warnings: 0
To check if the column values have been swapped or not.
mysql> select *from SwappingTwoColumnsValueDemo;
The following is the output.
+------------------+-------------------+ | FirstColumnValue | SecondColumnValue | +------------------+-------------------+ | 20 | 10 | | 40 | 30 | | 60 | 50 | | 80 | 70 | | 100 | 90 | +------------------+-------------------+ 5 rows in set (0.00 sec)
以上就是在MySQL中交换两列的值?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号