How Select SUM works (IF ('' , '', ''))

0

I'm just having a question about how select mysql works.

Select SUM(IF('','',''))

I'm using it but I do not know exactly how it works

    
asked by anonymous 02.06.2016 / 15:19

2 answers

2

Imagine your example: SUM(IF(debito_credito_financeiro = 'D', valor_financeiro, 0)) AS debito,

The syntax is

  

IF (condition, return if true, return if false)

That is, if debito_credito_financeiro has the value of D , then it will add valor_financeiro otherwise, it will add 0 .

    
02.06.2016 / 15:30
1

Let's say I want to count how many branches in my branch table have the greater than 20 code.

I could write:

select sum( if ( cod_filial > 21, 1, 0 )) from tabelafilais

or would like to count how many records start with 58 in the location table:

select sum( if ( cod_local like '58%', 1, 0 )) from tabela_locais
    
02.06.2016 / 15:40