Browse data from 1 day to 3 days ago mysql

Asked

Viewed 3,234 times

3

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

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;
  • 1

    Please add to the question an example of feedback that you expect. I confess that I found the question very confusing.

  • This sentence is not at all clear: "[...] but must be subtracted two days and only demonstrate the value of a single day before the 2 days [...]". Try to improve the explanation, or someone will hardly be able to help you.

3 answers

3

You can use in some ways, among them:

  • Passing the date you want by direct parameter by PHP:

    $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 desired number of days:

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

I hope it helps

Hug

  • Thanks for the help, but in the example mode is demonstrated all the data 3 days ago, what I would like is the value of a just a certain day 3 days ago, thank you.

1

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

The NOW() is the present day.

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

1

I’m not sure I got it right, but from what I understand query below takes the last 3 dates and you can determine which date return within these 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’ve helped you Abs

Browser other questions tagged

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