
- 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 all the fields in a table with null or non-null values with MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,NULL); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(NULL,'David'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(NULL,NULL); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output−
+------+-------+ | Id | Name | +------+-------+ | 10 | NULL | | NULL | David | | NULL | NULL | +------+-------+ 3 rows in set (0.00 sec)
Here is the query to update all the fields in a table with null or non-null values −
mysql> update DemoTable set Name='Robert' where Id IS NULL or Name IS NULL; Query OK, 3 rows affected (0.22 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable;
This will produce the following output −
+------+--------+ | Id | Name | +------+--------+ | 10 | Robert | | NULL | Robert | | NULL | Robert | +------+--------+ 3 rows in set (0.00 sec)
- Related Articles
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- Display first non-null values with coalesce() in MySQL?
- MySQL queries to update date records with NULL values
- How does COALESCE order results with NULL and NON-NULL values?
- Fetch maximum value from multiple columns with null and non-null values?
- Counting the number of non-null or nonzero columns in a MySQL table?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Perform MySQL SELECT on fields containing null values?
- Check for NULL or NOT NULL values in a column in MySQL
- MySQL update column to NULL for blank values
- Python - Remove a column with all null values in Pandas
- How do I update NULL values in a field in MySQL?
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query

Advertisements