Multiple if in one SQL condition

Asked

Viewed 558 times

0

I’m trying to use 2 ifbut the result always returns empty, when I use only one if he will.

SELECT aci_codigo, aci_valor_pagamento, bai_fk_pes_codigo,
       aci_tipo_pagamento, aci_fk_ftm_codigo, aci_nosso_numero,
       ftm_descricao, ftm_codigo, bai_fk_bor_codigo, 
       bor_tipo,bai_data_cadastro
FROM acordo_item
INNER JOIN baixa ON bai_fk_aci_codigo = aci_codigo
INNER JOIN bordero ON bor_codigo = bai_fk_bor_codigo
LEFT JOIN financeiro_tipo_movimentacao ON ftm_codigo = aci_fk_ftm_codigo
WHERE aci_status = 'B'
AND 
IF(aci_tipo_pagamento = "PE", bai_data_cadastro, bor_data_criacao) >= '2016-11-22 00:00:00'
AND 
IF(aci_tipo_pagamento = "PE", bai_data_cadastro, bor_data_criacao) <= '2016-11-22 00:00:00'
GROUP BY aci_codigo
  • What is the purpose of the aci_type payment constraint? Note that you built something like "A >= x and A <= x". The constraint will only be true if A = x. That is, only if the date selected is 11/22/2016.

2 answers

1


...
AND 
(
IF(aci_tipo_pagamento = "PE", bai_data_cadastro, bor_data_criacao) >= '2016-11-22 00:00:00'
AND 
IF(aci_tipo_pagamento = "PE", bai_data_cadastro, bor_data_criacao) <= '2016-11-22 00:00:00'
)
GROUP BY aci_codigo
  • How do these parentheses solve the problem???

  • But they solved ?

0

I was able to solve it this way

AND IF(aci_tipo_payment = 'PE', bai_data_registration >= '{$dataI} 00:00:00', bor_data_creation >= '{$dataI} 00:00:00') AND IF(aci_tipo_payment = 'PE', bai_data_cadastro <= '{$dataF} 23:59:59', bor_data_criacao <= '{$dataF} 23:59:59')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.