
- 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 multiple values in a table with MySQL IF Statement
Let us first create a table −
mysql> create table DemoTable716 ( Id varchar(100), Value1 int, Value2 int, Value3 int ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable716 values('100',45,86,79); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable716 values('101',67,67,99); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable716 values('102',77,57,98); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable716 values('103',45,67,92); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable716;
This will produce the following output -
+------+--------+--------+--------+ | Id | Value1 | Value2 | Value3 | +------+--------+--------+--------+ | 100 | 45 | 86 | 79 | | 101 | 67 | 67 | 99 | | 102 | 77 | 57 | 98 | | 103 | 45 | 67 | 92 | +------+--------+--------+--------+ 4 rows in set (0.00 sec)
Following is the query to update multiple values in a table −
mysql> update DemoTable716 set Value3=if(Value1=67 OR Value2=67,67,NULL) where Id='101'; Query OK, 1 row affected (0.14 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable716;
This will produce the following output -
+------+--------+--------+--------+ | Id | Value1 | Value2 | Value3 | +------+--------+--------+--------+ | 100 | 45 | 86 | 79 | | 101 | 67 | 67 | 67 | | 102 | 77 | 57 | 98 | | 103 | 45 | 67 | 92 | +------+--------+--------+--------+ 4 rows in set (0.00 sec)
- Related Articles
- SHOW TABLE statement with multiple LIKE values in MySQL?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- Update with multiple values in MySQL WHERE clause
- MySQL If statement with multiple conditions?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- Insert multiple sets of values in a single statement with MySQL?
- How can we update values in a MySQL table?
- Update a MySQL table with Java MySQL
- Insert multiple values in a temporary table with a single MySQL query?
- How we have multiple stored GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- Using Update statement with TINYINT in MySQL?
- What MySQL returns if sub-query, used to assign new values in the SET clause of UPDATE statement, returns multiple rows?
- Update all the fields in a table with null or non-null values with MySQL
- How can we have multiple virtuals GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- Update table with duplicate ids in MySQL

Advertisements