
- 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
How to update a MySQL table by swapping two column values?
To swap two values in a column, use CASE WHEN statement. Let us first create a table −
mysql> create table DemoTable1382 -> ( -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1382 values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1382 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1382 values('Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1382 values('Bob'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1382;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | John | | Chris | | Adam | | Bob | +-------------+ 4 rows in set (0.00 sec)
Following is the query to update a MySQL table by swapping two column values −
mysql> update DemoTable1382 -> set StudentName=( case when StudentName="Chris" then "John" -> when StudentName="John" then "Chris" -> else -> StudentName -> end -> ); Query OK, 2 rows affected (0.19 sec) Rows matched: 4 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1382;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | Chris | | John | | Adam | | Bob | +-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Swapping two column values in MySQL?
- How can we update the values in one MySQL table by using the values of another MySQL table?
- How can we update values in a MySQL table?
- Update MySQL table column by matching date using date() function?
- How can we update MySQL table after removing a particular string from the values of column?
- How can we update MySQL table after padding a string with the values of the column?
- MySQL query to get first two highest column values from a table?
- MySQL update column to NULL for blank values
- How can I update MySQL table after quoting the values of a column with a single quote?
- MySQL regular expression to update a table with column values including string, numbers and special characters
- Best way to update a single column in a MySQL table?
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- How can I create a stored procedure to update values in a MySQL table?
- How to update a MySQL column by subtracting a value with some conditions?
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?

Advertisements