
- 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 use MySQL CASE statement while using UPDATE Query?
For using MySQL CASE statement while using UPDATE Query, you can use CASE statement. Let us first create a table −
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserScore int ); Query OK, 0 rows affected (0.29 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(UserScore) values(100); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(UserScore) values(110); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(UserScore) values(120); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(UserScore) values(200); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(UserScore) values(230); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(UserScore) values(250); Query OK, 1 row affected (0.03 sec) mysql> insert into DemoTable(UserScore) values(270); Query OK, 1 row affected (0.06 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+-----------+ | UserId | UserScore | +--------+-----------+ | 1 | 100 | | 2 | 110 | | 3 | 120 | | 4 | 200 | | 5 | 230 | | 6 | 250 | | 7 | 270 | +--------+-----------+ 7 rows in set (0.00 sec)
Following is the query to use CASE statement while using UPDATE query −
mysql> update DemoTable set UserScore = CASE WHEN UserScore BETWEEN 100 AND 120 THEN UserScore + 5 WHEN UserScore BETWEEN 130 AND 230 THEN UserScore +10 WHEN UserScore >=250 THEN UserScore * 5 ELSE UserScore END; Query OK, 7 rows affected (0.06 sec) Rows matched: 7 Changed: 7 Warnings: 0
Now you can display all records from the table once again −
mysql> select *from DemoTable;
This will produce the following output −
+--------+-----------+ | UserId | UserScore | +--------+-----------+ | 1 | 105 | | 2 | 115 | | 3 | 125 | | 4 | 210 | | 5 | 240 | | 6 | 1250 | | 7 | 1350 | +--------+-----------+ 7 rows in set (0.00 sec)
- Related Articles
- How to use a select statement while updating in MySQL?
- Why should we use MySQL CASE Statement?
- How to use count with CASE condition in a MySQL query?
- Using Update statement with TINYINT in MySQL?
- MySQL order by field using CASE Statement
- How to update a specific column value fetched with CASE statement?
- Can we use WHERE clause inside MySQL CASE statement?
- How to use prepared statement for select query in Java with MySQL?
- How to use IF statement in MySQL using Python?
- Is it possible to use UPDATE query with LIMIT in MySQL?
- How can we change MySQL user password by using UPDATE statement?
- MySQL Query to change lower case to upper case?
- MySQL case statement inside a select statement?
- MySQL update query to remove spaces?
- How to implement WHILE LOOP with IF STATEMENT MySQL?

Advertisements