
- 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
MySQL UPDATE using IF condition
The syntax is as follows to perform UPDATE using IF condition in MySQL −
update yourTableName set yourColumnName =if(yourColumnName =yourOldValue,yourNewValue,yourColumnName);
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table updateIfConditionDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserName varchar(20), -> UserAge int -> ); Query OK, 0 rows affected (4 min 0.59 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into updateIfConditionDemo(UserName,UserAge) values('Larry',23); Query OK, 1 row affected (0.11 sec) mysql> insert into updateIfConditionDemo(UserName,UserAge) values('Mike',21); Query OK, 1 row affected (0.20 sec) mysql> insert into updateIfConditionDemo(UserName,UserAge) values('Sam',23); Query OK, 1 row affected (0.15 sec) mysql> insert into updateIfConditionDemo(UserName,UserAge) values('David',23); Query OK, 1 row affected (0.14 sec) mysql> insert into updateIfConditionDemo(UserName,UserAge) values('Maxwell',23); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from updateIfConditionDemo;
Here is the output −
+--------+----------+---------+ | UserId | UserName | UserAge | +--------+----------+---------+ | 1 | Larry | 23 | | 2 | Mike | 21 | | 3 | Sam | 23 | | 4 | David | 23 | | 5 | Maxwell | 23 | +--------+----------+---------+ 5 rows in set (0.00 sec)
Here is the query to update using IF condition −
mysql> update updateIfConditionDemo set UserAge =if(UserAge =23,26,UserAge); Query OK, 4 rows affected (0.20 sec) Rows matched: 5 Changed: 4 Warnings: 0
Let us check the table records once again. The UserAge has been updated from 23 to 26 −
mysql> select *from updateIfConditionDemo;
Here is the output −
+--------+----------+---------+ | UserId | UserName | UserAge | +--------+----------+---------+ | 1 | Larry | 26 | | 2 | Mike | 21 | | 3 | Sam | 26 | | 4 | David | 26 | | 5 | Maxwell | 26 | +--------+----------+---------+ 5 rows in set (0.00 sec)
- Related Articles
- Can we update MySQL with if condition?
- MySQL Stored Procedure to update records with certain condition?
- MySQL Sum Query with IF Condition using Stored Procedure
- MySQL query to update different fields based on a condition?
- MySQL Sum Query with IF Condition?
- Update an array element matching a condition using $push in MongoDB
- MongoDB query to update an array element matching a condition using $push?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- How to remove hyphens using MySQL UPDATE?
- Using Update statement with TINYINT in MySQL?
- How to use if/else condition in select in MySQL?
- Update only a single column in a MySQL table and increment on the basis of a condition
- Update column data without using temporary tables in MySQL?
- Set special characters on values if condition is true in MySQL?
- Does UPDATE overwrite values if they are identical in MySQL

Advertisements