
- 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 a column A if null, else update column B, else if both columns are not null do nothing with MySQL
For this, use IF() with IS NULL property. Let us first create a table −
mysql> create table DemoTable1976 ( FirstName varchar(20), LastName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1976 values('John','Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1976 values('John',NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1976 values(NULL,'Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1976 values('Chris','Brown'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1976;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Doe | | John | NULL | | NULL | Miller | | Chris | Brown | +-----------+----------+ 4 rows in set (0.00 sec)
Here is the query to update a column if null else update anther column, else if both columns are not null do nothing −
mysql> update DemoTable1976 set FirstName=if(FirstName IS NULL,'David',FirstName), LastName=if(LastName IS NULL,'Brown',LastName); Query OK, 2 rows affected (0.00 sec) Rows matched: 4 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1976;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Doe | | John | Brown | | David | Miller | | Chris | Brown | +-----------+----------+ 4 rows in set (0.00 sec)
- Related Articles
- Sum if all rows are not null else return null in MySQL?
- Update one column data to another column in MySQL if the second column is NOT NULL?
- Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL
- MySQL update column to NULL for blank values
- Insert default into not null column if value is null in MySQL?
- MySQL Mass Update with CASE WHEN/ THEN/ ELSE?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- CASE WHEN column1 IS NULL THEN NULL ELSE column2 END with MySQL
- Only insert if a value is unique in MongoDB else update
- SELECT not null column from two columns in MySQL?
- How to update a field with a particular value if it is null in MySQL?
- How do I check if a column is empty or null in MySQL?
- Check for NULL or NOT NULL values in a column in MySQL
- Update a MySQL column with JSON format?
- How do I update NULL values in a field in MySQL?

Advertisements