I have a QUERY that doesn’t display what I really need

Asked

Viewed 54 times

-5

My Query:

    select id from clientes where cliente = '$cliente' AND status = '2' AND
(tipo <> '0' AND (disponibilidade <> '0'  OR vanual <> '0' OR vtemporada <> '0'))

I need it to work this way:

Which displays some record if the field TYPE for non-zero and that at least one of the camps: availability, vanual and vtemporada have any value other than zero.

That is, for it to display some record the field TYPE it is obligatory to be different from ZERO and also some of the fields availability, vanual and vtemporada have any value other than zero

Only this SQL I built doesn’t do that!

I’m waiting for help please!

  • There is already a topic where you asked the same thing, haha. Try to use it yourself to try to solve. I’m even posting there trying to help you. This is the link to the other post: http://answall.com/questions/118230/comorvalidar-alguns-campos-em-sql/

  • Put the table structure.

2 answers

2

Modifying your query, which I believe is being assigned to some variable before it is executed, try it this way:

$sql = "SELECT id 
FROM clientes 
WHERE
cliente = $cliente 
AND status = 2
AND tipo <> 0 
AND (disponibilidade <> 0 OR vanual <> 0 OR vtemporada <> 0)"

1

After talking, through the chat, I saw that what you really want is: If the tipo is zero, it already accounts. If the tipo is non-zero, checks that other information is zero, and accounts for.

This being exactly what you need, I believe that the following consultation will return the expected:

SELECT 
    COUNT(1) as TOTAL_CLIENTES /*Conte 1 exibindo coluna como TOTAL_CLIENTES*/ 
    FROM 
    clientes /*Da tabela de clientes*/ 
    WHERE 
    (tipo = 0 ) /* Se o tipo for zero (só ai o cadastro já está incompleto)*/
    OR (/*OU*/
        tipo <> 0 /*Existe um tipo informado*/
        AND disponibilidade = 0 /*E a disponibilidade é zero*/
        AND vvenda = 0 /* E o vvenda é zero também*/
        AND vtemporada = 0 /* E o vtemporada também é zero, caracterizando um cliente que tem um tipo mas não tem nenhuma informação de valores*/
    )

I copied the answer from the other created topic.

Browser other questions tagged

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