MySQL : How to fill in empty data with custom text

MySQL

I have the following data :

SELECT
   city,
   region,
   postcode
FROM
   AREA
WHERE
   city IN ( 'Jakarta', 'Jabodetabek' )

with the result :

enter image description here

I want the result like this

enter image description here

I've tried several tutorials, including here

https://stackoverflow.com/questions/17832906/how-to-check-if-field-is-null-or-empty-in-mysql

but the result is the same as the first image

Best Answer

You must convert your values list to rowset.

For example:

SELECT
   city,
   area.region,
   area.postcode
FROM ( SELECT 'Jakarta' city UNION ALL
       SELECT 'Jabodetabek' ) cities 
LEFT JOIN area USING (city)

This query will give NULL for the row for which there is no city value in area table. If you need some custom string literal then use COALESCE() function, i.e. not SELECT city, ... but SELECT COALESCE(city, 'Empty') city, ....


Depends on MySQL version you may use another methods: ROW() function, JSON_TABLE() function, etc.