
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Swap data between two columns in MySQL?
To swap data between two columns in MySQL, use the concept of variable. Let us first create a table. Here, we will swap Name1 with Name2 −
mysql> create table DemoTable -> ( -> Name1 varchar(100), -> Name2 varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John Smith','Chris Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David Miller','Jone Doe'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------------+-------------+ | Name1 | Name2 | +--------------+-------------+ | John Smith | Chris Brown | | David Miller | Jone Doe | +--------------+-------------+ 2 rows in set (0.00 sec)
Following is the query to swap data between two columns in MySQL −
mysql> update DemoTable -> SET Name1=(@tmpName:=Name1), Name1 = Name2, Name2 = @tmpName; Query OK, 2 rows affected (0.13 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-------------+--------------+ | Name1 | Name2 | +-------------+--------------+ | Chris Brown | John Smith | | Jone Doe | David Miller | +-------------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to move data between two tables with columns in different MySQL databases?
- Sorted difference between two columns in MySQL?
- Fetch data between two rows in MySQL?
- Concatenate two columns in MySQL?
- How to get the difference between two columns in a new column in MySQL?
- How to search between columns in MySQL?
- Find “greatest” between two columns and display with some records already null in MySql
- Select distinct combinations from two columns in MySQL?
- Select distinct values from two columns in MySQL?
- Custom sorting using two different columns in MySQL?
- Swap two numbers in C#
- SELECT MySQL rows where today's date is between two DATE columns?
- Order a MySQL table by two columns?
- MYSQL select DISTINCT values from two columns?
- SELECT not null column from two columns in MySQL?

Advertisements