
- 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
Can we update MySQL with if condition?
You can update MySQL with IF condition as well as CASE statement. For this purpose, let us first create a table. The query to create a table −
mysql> create table UpdateWithIfCondition −> ( −> BookId int, −> BookName varchar(200) −> ); Query OK, 0 rows affected (0.60 sec)
Now you can insert records in the table using insert command. The query is as follows −
mysql> insert into UpdateWithIfCondition values(1000,'C in Depth'); Query OK, 1 row affected (0.12 sec) mysql> insert into UpdateWithIfCondition values(1001,'Introduction to Java'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from UpdateWithIfCondition;
The following is the output −
+--------+----------------------+ | BookId | BookName | +--------+----------------------+ | 1000 | C in Depth | | 1001 | Introduction to Java | +--------+----------------------+ 2 rows in set (0.00 sec)
Let us update the value ‘C in Depth’ with the value 'Introduction to C' and 1001 with the value 2000 using if condition. The query is as follows −
mysql> update UpdateWithIfCondition −> set BookName = if(BookName = 'C in Depth','Introduction to C',BookName), −> BookId = if(BookId = 1001,2000,BookId); Query OK, 2 rows affected (0.43 sec) Rows matched: 2 Changed: 2 Warnings: 0
Above, we have updated both the column values. The following is the query to check both the two values have been updated or not −
mysql> select *from UpdateWithIfCondition;
The following is the output displaying that we have successfully updated the values using if −
+--------+----------------------+ | BookId | BookName | +--------+----------------------+ | 1000 | Introduction to C | | 2000 | Introduction to Java | +--------+----------------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL UPDATE using IF condition
- MySQL Stored Procedure to update records with certain condition?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- MySQL Sum Query with IF Condition?
- How can we copy data with some condition/s from existing MySQL table?
- How can we update values in a MySQL table?
- Can we update a row with the highest ID in a single MySQL query?
- MySQL Sum Query with IF Condition using Stored Procedure
- How can we change MySQL user password by using UPDATE statement?
- Can we perform MySQL UPDATE and change nothing in a table?
- How can we update MySQL table after padding a string with the values of the column?
- MySQL query to update different fields based on a condition?
- Truncate with condition in MySQL?
- Counting with condition in MySQL?

Advertisements