
- 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 query to set values for NULL occurrence
Find NULL values using the IS NULL and update the new values using MySQL UPDATE and SET −
update yourTableName set yourColumnName=yourValue where yourColumnName IS NULL;
Let us first create a table −
mysql> create table DemoTable768 ( Clientid int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientAge int ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable768(ClientName,ClientAge) values('John',23); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,26); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable768(ClientName,ClientAge) values('Carol',28); Query OK, 1 row affected (0.53 sec) mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,24); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable768;
This will produce the following output -
+----------+------------+-----------+ | Clientid | ClientName | ClientAge | +----------+------------+-----------+ | 1 | John | 23 | | 2 | NULL | 26 | | 3 | Carol | 28 | | 4 | NULL | 24 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
Following is the query to set values for NULL occurrence −
mysql> update DemoTable768 set ClientName='Chris' where ClientName IS NULL; Query OK, 2 rows affected (0.41 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the description of the view −
mysql> select *from DemoTable768;
This will produce the following output -
+----------+------------+-----------+ | Clientid | ClientName | ClientAge | +----------+------------+-----------+ | 1 | John | 23 | | 2 | Chris | 26 | | 3 | Carol | 28 | | 4 | Chris | 24 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to order by NULL values
- MySQL query to convert empty values to NULL?
- Set value only for NULL values in a MySQL table
- MySQL query to set different combinations for values in a table?
- Set a custom value for NULL or empty values in MySQL
- How to concat Values in MySQL Query and to handle Null values as well?
- MySQL query to replace only the NULL values from the table?
- MySQL query to display only the empty and NULL values together?
- MySQL query to select the values having multiple occurrence and display their count
- MySQL update column to NULL for blank values
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- MySQL query to compare and display only the rows with NULL values
- Display 1 for NULL values in MySQL
- How to use a single MySQL query to count column values ignoring null?
- Set JavaScript object values to null?

Advertisements