
- 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
Update column data without using temporary tables in MySQL?
For this, use CASE statement. This will work even without using temporary tables. Let us first create a table −
mysql> create table DemoTable -> ( -> UserName varchar(100), -> UserStatus varchar(100) -> ); Query OK, 0 rows affected (0.74 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','Active'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('Chris','Inactive'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Bob','Inactive'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Robert','Active'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----------+------------+ | UserName | UserStatus | +----------+------------+ | John | Active | | Chris | Inactive | | Bob | Inactive | | Robert | Active | +----------+------------+ 4 rows in set (0.00 sec)
Following is the query to update column data using CASE −
mysql> update DemoTable -> set UserStatus=CASE UserStatus WHEN 'Inactive' THEN 'Active' ELSE 'Inactive' END; Query OK, 4 rows affected (0.28 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----------+------------+ | UserName | UserStatus | +----------+------------+ | John | Inactive | | Chris | Active | | Bob | Active | | Robert | Inactive | +----------+------------+ 4 rows in set (0.00 sec)
- Related Articles
- Set particular value of column without using update and while inserting values in MySQL
- How can we see MySQL temporary tables in the list of tables?
- UPDATE column to append data into it in MySQL?
- How to update data in a MySQL database without removing the old data?
- Fix Error with TYPE=HEAP for temporary tables in MySQL?
- What happens to MySQL temporary tables if MySQL session is ended?
- Update one column data to another column in MySQL if the second column is NOT NULL?
- What are MySQL Temporary Tables? How can we create them?
- Second approach to fetch data from SAP tables without using SAP JCo
- Add a temporary column with a value in MySQL?
- Swap Numbers without using temporary variable in Swift Program?
- How can I see the description of a MySQL Temporary Tables?
- How to change the column position of MySQL table without losing column data?
- Update MySQL table column by matching date using date() function?
- Get MAX() on column in two MySQL tables?

Advertisements