Date Posted: 05-04-2018

In this we will explain MySql replace NULL values with empty string.

Step1: You can create two different tables. using the following query.

CREATE TABLE `users` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `username` varchar(16) NOT NULL,
 `name` varchar(40) DEFAULT NULL,
 `last_name` varchar(60) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO `users`(username,name,last_name) VALUES ('xxx','xyz','abc');

CREATE TABLE `billings` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` int(11) NOT NULL,
 `address` varchar(16) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

INSERT INTO `billings`(user_id,address) VALUES ('1','');

Step2: We can use mysql IFNULL function to replace the null values with empty string.

Synatx:

IFNULL(null,"") as value 

Example:

SELECT u.*,IFNULL(b.address,"") AS address FROM `users` as u LEFT JOIN billings as b on b.user_id=u.id  order by u.id

 

 

Leave a Reply

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny