Is it possible to use if else in MySQL queries?

1

I've been researching the internet and hearing rumors that it's possible to use if else in MySQL queries but have not seen anything concrete about it. Is it really possible? If so, how?

I want to make a query in three distinct tables in search of the column name I have the following query as an example

SELECT 'PAIS'.nome, 'FILHOS'.nome, 'NETOS'.nome FROM 'PAIS', 'FILHOS', 'NETOS' WHERE 'PAIS'.nome LIKE '%MATILDE%' OR 'FILHOS'.nome LIKE '%MATILDE%' OR 'NETOS'.nome LIKE '%MATILDE%'

This query will query the three tables PAIS, FILHOS, NETOS searching for '%MATILDE%' in the name field

Now I want to do as in this example but with SE

SE encontrar '%MATILDE%' na tabela NETOS CONCATENAR
CONCAT(' É FILHA DE ', FILHOS.nome),
CONCAT(' E NETA DE ', PAIS.nome)

SE encontrar '%MATILDE%' na tabela PAIS CONCATENAR
CONCAT(' É MAE DE ', FILHOS.nome),
CONCAT(' E AVO DE ', NETOS.nome)

I want to implement something like this.

    
asked by anonymous 15.06.2016 / 00:38

3 answers

4

In the type of query you intend to do, you can resolve it with a subquery:

set @nome:='MATILDE';

SELECT CONCAT(' É MÃE DE ', f.nome) as filha,
       CONCAT(' É FILHA DE ', p.nome) as mae,
       CONCAT(' É AVÓ DE ',(SELECT nome from PAIS where nome like '%@nome%')) as neta,
       CONCAT(' É NETA DE ',p.nome) as avo,
    FROM 'PAIS' p, 
         'FILHOS' f,
         'NETOS' n 
  WHERE 'p'.nome LIKE '%@nome%'
     OR 'f'.nome LIKE '%@nome%' 
     OR 'n'.nome LIKE '%@nome%';

And use the IF condition to ignore the concatenation, as there may not always be daughters, granddaughters, grandmothers in the same database, you can do a filter:

   SELECT  IF(f.nome IS NOT NULL, CONCAT(' É MÃE DE ', f.nome), null) as filha,
           IF(p.nome IS NOT NULL, CONCAT(' É FILHA DE ', p.nome), null) as mae,
           IF((SELECT nome from NETOS where nome like '%@nome%') IS NOT NULL, CONCAT(' É AVÓ DE ',(SELECT nome from NETOS where nome like '%@nome%'), null) as neta,
           IF(p.nome IS NOT NULL, CONCAT(' É NETA DE ',p.nome),null) as avo,
        FROM 'PAIS' p, 
             'FILHOS' f,
             'NETOS' n 
      WHERE 'p'.nome LIKE '%@nome%'
         OR 'f'.nome LIKE '%@nome%' 
         OR 'n'.nome LIKE '%@nome%';

Obs: Your question was confused because in the relationship you present, it was not clear what differentiates the "granddaughter" in the relationship between parents and daughters. For grandchildren can be mothers as well as daughters. Even if I try the same surname, a mother may come as being granddaughter of herself if she considers only the name relationship.

    
15.06.2016 / 16:22
3

This is a function almost equal to what exists in Excel, or as we are more accustomed in programming, it works analogous to a operator conditional . The ELSE is consequence because the first argument of the function is the condition, the second is the expression that it should return if the condition is true and the third argument is what will return if the condition is false, with what would be executed "no ELSE ."

Documentation .

So it is not used as a flow control effectively, but only as part of a calculation of values. You can only do simple things.

Bacco's comment has actual use example .

There is IF/ELSE for flow control , but not for use in SQL queries, it is to program functions and procuderes .

There you can see that you can use a CASE also that works in a similar way, but you can have several options, that is, for each value found in the main expression (probably one column) there will be a return of another value corresponding. It is possible to use ELSE in it too.

So I would not call it a rumor, it was information passed by someone being give greater grounds and references. Whenever possible consult the official documentation. Or ask here:)

    
15.06.2016 / 01:01
2

In SQL it is possible to use CASE ELSE

CASE WHEN aaa = bbb THEN xxx ELSE zzzz END

Example:

SELECT a, b, c, (CASE WHEN aaa = bbb THEN xxx ELSE zzzz END) AS d
FROM table

It is compatible with SQL 92, it works in SQL SERVER and MySQL

    
15.06.2016 / 01:30