Query data from 1 day to 3 days ago mysql [closed]

2

I'm trying to show records for the last 3 days, but it should be subtracted two days and only demonstrate the value of a single day before the 2 days, I currently have the following code that is demonstrated the value of the last month subtracting the month current.

ProdValor  | Data
250,00     | 01/04/15
150,00     | 02/04/15
50,00      | 03/04/15

Code:

 $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;
    
asked by anonymous 03.04.2015 / 17:39

3 answers

2

You can use it in some ways, among them:

  • Passing the date you want by PHP directly:

    $pesquisa = mysql_query("SELECT sum(ProdValor) FROM vendas WHERE data = '".date('Y-m-d')."'");
    
    $pesquisa = mysql_query("SELECT sum(ProdValor) FROM vendas WHERE data = '".$data."'");
    
  • Subtracting the amount of days you want:

    $pesquisa = mysql_query("SELECT sum(ProdValor) FROM vendas WHERE data = DATE_SUB(CURDATE(),INTERVAL 3 DAY)");
    

Hope it helps

Embrace

    
03.04.2015 / 20:32
1
SELECT
   SUM(PRODVALOR) AS TOTAL,
   DATA
WHERE
   DATA BETWEEN DATE_ADD(DATA, INTERVAL - 3 DAY) AND NOW()
ORDER BY
   DATA ASC
LIMIT 1

NOW() is the current day.

In this query, the first record is selected three days ago.

    
25.11.2015 / 20:45
1

I'm not sure I understand right, but from what I understand the query below it takes the last 3 dates and you can determine which date to return within those 3 days.

SELECT * FROM (SELECT row_number() OVER (ORDER BY v.DATA DESC) n, v.* FROM vendas v) T
WHERE T.N <= 3
AND T.DATA = $data

I hope I have helped you Abs

    
06.05.2015 / 20:56