How to show a column, but if it has null show another sql

3

How to show a column, but if it has null show another sql such as a table named Task.

| id  |  nome    |  nome resumido |
+-----+----------+----------------+
| 1   | nome 1   | 1              |
| 2   | nome 2   | 2              |
| 3   | nome 3   | null           |

sql will get the short name, where it is null it takes the name for example:

|  id | as nome  |
+-----+----------+
| 1   |  1       |
| 2   |  2       |
| 3   |  nome 3  |

If you can not get into sql I'm using the language in php

    
asked by anonymous 30.05.2018 / 16:36

3 answers

5

As commented on in the question itself, it can be done using the COALESCE :

SELECT id, COALESCE(nome_resumido, nome) as nome
FROM nome_tabela

You can also use the IFNULL :

SELECT id, IFNULL(nome_resumido, nome) as nome
FROM nome_tabela

In addition to these, as suggested in another answer, it can be done using CASE :

SELECT id, 
    CASE nome_resumido is null THEN nome
    ELSE nome_resumido END as nome
FROM nome_tabela
    
30.05.2018 / 17:32
2

Friend you can use mysql CASE:

SELECT nome, nome_resumido,
  CASE
    WHEN nome_resumido IS NULL OR nome_resumido = '' THEN nome
    ELSE nome_resumido
  END AS Nome
FROM Users;

I hope to have helped my friend.

    
30.05.2018 / 16:42
0

I do not know if I understood better.

Using ISNULL / function operator.

Select ISNULL(a, b)

b is selected if a for Null.

You can even use WHEN / THEN a Select option, look for the "BOL" better.

Essentially: It's the c switch / case block in SQL.

    
30.05.2018 / 16:47