MySQL IF NOT NULL, then display 1, else display 0

MysqlIf Statement

Mysql Problem Overview


I'm working with a little display complication here. I'm sure there's an IF/ELSE capability I'm just overlooking.

I have 2 tables I'm querying (customers, addresses). The first has the main record, but the second may or may not have a record to LEFT JOIN to.

I want to display a zero if there is no record in the addresses table. And I want to only display 1, if a record exists.

What I've attempted so far:

SELECT c.name, COALESCE(a.addressid,0) AS addressexists
FROM customers c
LEFT JOIN addresses a ON c.customerid = a.customerid
WHERE customerid = 123

This first example does not do it. But I may be utilizing COALESCE wrong.

How can I display a 0, if null, and a 1, if something exists?

Mysql Solutions


Solution 1 - Mysql

Instead of COALESCE(a.addressid,0) AS addressexists, use CASE:

CASE WHEN a.addressid IS NOT NULL 
       THEN 1
       ELSE 0
END AS addressexists

or the simpler:

(a.addressid IS NOT NULL) AS addressexists

This works because TRUE is displayed as 1 in MySQL and FALSE as 0.

Solution 2 - Mysql

SELECT c.name, IF(a.addressid IS NULL,0,1) AS addressexists
FROM customers c
LEFT JOIN addresses a ON c.customerid = a.customerid
WHERE customerid = 123

Solution 3 - Mysql

Careful if you're coming from C/C++ and expecting this to work:

select if(name, 1, 0) ..

Even if 'name' is not NULL, unlike in C, a false-condition still triggers and the above statement returns 0. Thus, you have to remember to explicitly check for NULL or empty string:

 select if(name is null or name = '', 0, 1)

PS Eugen's example up above is correct, but I wanted to clarify this nuance as it caught me by surprise.

Solution 4 - Mysql

SELECT 
    c.name, 
    CASE WHEN a.addressid IS NULL THEN 0 ELSE 1 END AS addressexists
FROM customers c
LEFT JOIN addresses a ON c.customerid = a.customerid
WHERE customerid = 123

Solution 5 - Mysql

Another method without WHERE, try this..

Will select both Empty and NULL values

SELECT ISNULL(NULLIF(fieldname,''))  FROM tablename

It will set null if it is an empty string, then be true on that also.

Solution 6 - Mysql

You can actually use an IF statement in the latest versions of MySQL.

IF(expr,if_true_expr,if_false_expr)

IE:

SELECT name, IF(ISNULL(name), 'robot', 'human') AS type
FROM visitors

Solution 7 - Mysql

If within TSQL, you can try :

SELECT IIF(a.addressid IS NULL, 0, 1) AS addressexists

SQL Server should work

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoffeemonitorView Question on Stackoverflow
Solution 1 - MysqlypercubeᵀᴹView Answer on Stackoverflow
Solution 2 - MysqlEugen RieckView Answer on Stackoverflow
Solution 3 - MysqlSlawomirView Answer on Stackoverflow
Solution 4 - MysqlbuckoView Answer on Stackoverflow
Solution 5 - MysqlPodTech.ioView Answer on Stackoverflow
Solution 6 - MysqlSalvi PascualView Answer on Stackoverflow
Solution 7 - MysqlKevin ChenView Answer on Stackoverflow