2
I am trying to run the following subquery within a view:
(SELECT
if(idnotas > 0, "não", "sim")
FROM notas WHERE vendas_idvendas = p.vendas_idvendas
ORDER BY idnotas DESC
LIMIT 1
) as primeiraFatura,
However, if is only defined when the condition is true. The moment she’s fake, I get a null, instead of yes.
The funny thing is that if I run the same as a normal query ( without being a subquery ) the return is exactly what I want, and the value "yes" is assigned ( when it matches the condition ).
Follow the full query:
SELECT
idpatrimonios, numeroPatrimonio, n.dataVencimento,
(SELECT nomeTipo FROM tipos WHERE idtipos = p.tipos_idtipos) as tipo,
cl.idclientes as idclientes, p.vendas_idvendas, dataEntrega, dataFatura,
(
SELECT
if(idnotas > 0, "não", "sim")
FROM notas WHERE vendas_idvendas = p.vendas_idvendas
ORDER BY idnotas DESC
LIMIT 1
) as primeiraFatura,
(
SELECT
case
WHEN MONTH(dataVencimento) >= MONTH(NOW()) THEN "não"
WHEN NOW() >= dataFatura THEN "sim"
end
FROM notas WHERE vendas_idvendas = p.vendas_idvendas
ORDER BY idnotas DESC
LIMIT 1
) as exibirNaLista
FROM patrimonios as p
INNER JOIN vendas as v ON v.idvendas = p.vendas_idvendas
INNER JOIN clientes as cl ON cl.idclientes = v.clientes_idclientes
LEFT JOIN logpatrimonios as log ON log.patrimonios_idpatrimonios = p.idpatrimonios
LEFT JOIN notas as n ON n.vendas_idvendas = p.vendas_idvendas
WHERE situacao_idsituacao = 2
group by idpatrimonios;
I am using Mysql 8.
have tried using single quotes :
– Marco Souza
Yeah, it’s not the problem.
– Vitor Morais
Perhaps it would be better to validate if the field is
NULL
and assign a value:if(ifnull(idnotas. 0) > 0, "não", "sim")
– João Martins
It doesn’t help me much, but I tested only for consciousness disengagement and the return remains null: if(ifnull(idnotas, 0) > 0, "no", "yes")
– Vitor Morais
SELECT IF(COUNT(idnotes) > 0, 'no', 'yes') FROM notes...
– FabioIn
Fabius thus solved the problem. But, I still continue with the question at other points of the query that return null when the first condition is not satisfied, for example in the return of 'displayNaLista'. I need to understand what to do with these null returns so that the tests continue in case the condition is not satisfied.
– Vitor Morais
Vitor, the first condition?!? Sorry, question: What is the first condition? It would be "no"??
– FabioIn
I’ll take it you’re asking me about the 'show-off' thing. I have two cases: WHEN MONTH(due date) >= MONTH(NOW()) THEN 'no' WHEN NOW() >= dataFatura THEN 'yes' If the due date month is greater than or equal to the current date month, I want to display no. Up to this point it works perfectly. The problem happens if the due month is less than the current month, where I never enter the second case, for some reason
– Vitor Morais
Victor, it may be the following: The two conditions are NOT met.
– FabioIn
This is not the case, Fabio. As I answered your comment, even putting a ELSE, I still get null. And, as described in the question, if I run the subquery individually, I get the data I normally want.
– Vitor Morais
Vitor, I’ve looked at some examples at w3schools.com and I’m guessing that the "displaying them" is in the wrong position... please check.
– FabioIn