Condition in Mysql for data query

Asked

Viewed 148 times

1

Is there any way to make a condition to demonstrate data only if the status be as aprovado in the database?

Mysql:

Nome  ProdValor   Data         Status
Goku  250,00      15/04/2015   Aprovado
Goku  250,00      15/04/2015   Pendente
Goku  250,00      15/04/2015   Cancelado

PHP

 $busca = mysql_connect("$local","$usuario","$senha") or die("ERRO AO CONECTAR AO MYSQL, VERIFIQUE COM O ADMINISTRADOR" . mysql_error());
 mysql_select_db("$banco") or die("BASE DE DADOS INVÁLIDO");
 $pesquisa = mysql_query("SELECT sum(ProdValor) FROM vendas WHERE MONTH(data) = MONTH(DATE_ADD(CURDATE(),INTERVAL -1 MONTH))");

  while($sum = mysql_fetch_array($pesquisa)){
      $soma2 = $sum['sum(ProdValor)'];
  }    

  echo $soma2;

1 answer

4


Yes, it is possible. It would be something like this:

SELECT SUM(ProdValor) FROM vendas
    WHERE MONTH(data) = MONTH(DATE_ADD(CURDATE(),INTERVAL -1 MONTH)) AND Status = 'Aprovado'

I put in the Github for future reference.

As you have not given many details, it may be necessary some adaptation to what you will do. Ideally the column of Status were it not text.

  • I tested your example and it worked perfectly, thank you. The field I’m using is varchar.

  • I’ve marked it as more correct, thank you.

Browser other questions tagged

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