How to validate some fields in SQL

Asked

Viewed 116 times

-2

I have the following SQL

$contarperfilclienteinsuficiente = "0";
$sqlxmlperfilclienteinsuficiente = "select 
  COUNT(CASE WHEN tipo = 0 THEN 1 ELSE NULL END),
  COUNT(CASE WHEN tipo <> 0 THEN 1 ELSE NULL END)
from 
  clientes 
where 
  cliente = '$cliente' 
  AND status = '2' 
  AND (disponibilidade <> '0'  OR vanual <> '0' OR vtemporada <> '0')";
$rsqlxmlperfilclienteinsuficiente = mysql_query($sqlxmlperfilclienteinsuficiente)
or die ("Banco XML não abre!");
while($rowxmlperfilclienteinsuficiente = mysql_fetch_array($rsqlxmlperfilclienteinsuficiente))
   {
   $contarperfilclienteinsuficiente = $contarperfilclienteinsuficiente + 1;
   }  

I need him to count how many clients have the TYPE = 0 and also have the fields availability And vanual And vtemporada also zero

Important! Always the field TYPE IS PARAMOUNT TO BE ZERO to display a record, but if the field TYPE is non-zero And at least one of the camps availability OR vanual OR vtemporada are different from ZERO, NO record may be displayed as a result.

I have this other SQL that also does not work:

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

Help me please!

See the implemented model!

$sqlxmlperfilclienteinsuficiente = "SELECT
    COUNT(1) as TOTAL_CLIENTES 
FROM 
    CLIENTES 
WHERE 
    status = 2 
    AND 
    ( 
      ( tipo = 0     
        AND disponibilidade = 0 
        AND vanual = 0 
        AND vtemporada = 0 
      )
      OR
      ( tipo <> 0
        AND (disponibilidade <> 0 OR vanual <> 0 OR vtemporada <> 0)
      )
    )";
$rsqlxmlperfilclienteinsuficiente = mysql_query($sqlxmlperfilclienteinsuficiente)
or die ("Banco XML não abre!");
$num_rowsclienteinsuficiente = mysql_num_rows($rsqlxmlperfilclienteinsuficiente);
  • 1

    The query is to return the total of customers, right? But I noticed that you pass a customer ( $customer), which would restrict the universe to this customer only.

  • that’s right!!!!! Give me a hand in restructuring this second SQL I posted.

  • Okay, so you want to return the total of customers, where the type is equal to zero and the availability, the vanual and the vtemporada can not be different from zero, is that right? I was in doubt regarding these fields (availability, vanual and vtemporada)

  • @Márciolordelo It has to return record if the field type is equal to zero and if all fields availability, vanual and vtemporada are zero. But... will not return record if the capo type is non-zero and also if any of the fields availability, vanual and vtemporada are different from zero.

  • Instead of making more than one post with the same problem, you can edit the existing post and add the details that were missing for a suitable solution. The ideal is to already make the initial posting with the maximum details.

1 answer

-2

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 changed the post based on what we discussed in the chat.

  • Not really! It will only show results if the type is zero, availability is zero, vvenda is zero, vtemporada is zero. But if type is non-zero and some field as: availability, vsale or vtempoarada is non-zero will display the result.

  • Okay, let me get this straight. You want the query to return (return, means to count the client) if: - The type is equal to zero, availability zero, vanual zero and vtemporada zero; - The type is DIFFERENT from zero and, at least one between availability, vanual and vtemporada is different from zero. That’s right?

  • That’s right, straight up

  • How do I make this SQL?

  • I’m editing the post, a moment.

  • Gave error friend, sql does not run!

  • I will implement my text above for you to follow.

  • You saw my implementation above?

  • Okay, which error returns?

  • XML database does not open!

  • Please vote for the post if it has been helpful

Show 7 more comments

Browser other questions tagged

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