简体   繁体   中英

Displaying daily totals between now and x days ago using MYSQL/PHP

I have a table called 'orders' and it contains; id, order_total and time fields. 'time' is an integer and stores a unix timestamp...

orders

| id | order_total  | time          |
-------------------------------------
| 1  | 42.00        | 1443355834    |
| 2  | 13.00        | 1443460326    |
| 3  | 51.00        | 1443468094    |
| 4  | 16.00        | 1443477442    |
| 5  | 10.00        | 1443606966    |
| 6  | 53.00        | 1443608256    |

I want to able to display in a table using php the sum, of 'order_total' for each day for the previous 'x' amount of days (or weeks or months) so it will look something like this:

| Date      | Order Total |
---------------------------
| 27/09/15  | 42.00       |
| 28/09/15  | 80.00       |
| 30/09/15  | 63.00       |

I have made a MYSQL query and a php loop that kind of works but being new to MYSQL I am probably over-complicating things and there must be an easier way to do this ? When I say kind of works, it will correctly sum and show the order_totals up until the current day but for some reason will combine the current day with the previous day.

Here is what I currently have:

$x = $interval;
$y = $x - 1;

while ($x > 0) {
    $sql10 = "
        SELECT id,
               time,
               SUM(order_total) as sum,
               date_format(DATE_SUB(FROM_UNIXTIME($now_time), INTERVAL $x DAY), '%Y-%m-%d') as thedate  
         FROM $ordersTABLE 
        WHERE FROM_UNIXTIME(time) BETWEEN date_format(DATE_SUB(FROM_UNIXTIME($now_time), INTERVAL $x DAY),'%Y-%m-%d') 
          AND date_format(DATE_SUB(FROM_UNIXTIME($now_time), INTERVAL $y DAY),'%Y-%m-%d')";

    $result10 = mysql_query ( $sql10, $cid ) or die ( "Couldn't execute query." );

    while ( $row = mysql_fetch_array ( $result10) ) {
        $order_total = $row ["order_total"];
        $thedate = $row ["thedate"];
        $sum = $row ["sum"];
        $sum = number_format($sum,2);
        $thedate = strtotime($thedate);
        $thedate = date("d/m/y",$thedate);

        print "<tr><td width=\"120\">$thedate</td><td>\$$sum</td></tr>";
    }

    $x--;
    $y--;
}

(The string $now_time contains the current time as a Unix Timestamp hence the converting as the system time can not be changed and this contains the correct local time for the user)

Is there better way to do this ?

You can convert the timestamps into YYYY MM DD using FROM_UNIXTIME function and then select only the ones which are older enough thanks to the DATEDIFF function. Today's date is provided by CURDATE function.

First of all, the query which retrieves the totals for the orders older then the interval and reformats the date fields:

$q1 = "SELECT " . $ordersTABLE . ".order_total AS total, FROM_UNIXTIME(" . $ordersTABLE . ".time, '%Y-%m-%d') AS short_date FROM " . $ordersTABLE . " WHERE DATEDIFF(CURDATE(), short_date) > " . $intervalInDAYS;

Then, the one that sums up the totals of the day:

$q2 = "SELECT short_date AS day, SUM(total) AS total FROM (" . $q1 . ") GROUP BY short_date";

And then you perform your query stored in $q2 and all other operations you need to display the result.
Result from the query should be in form:

|    day    |    total    |
===========================
| 25/09/15  |  34.00      |
| 16/09/15  | 100.00      |
| 31/07/14  |   3.20      |
|    ...    |     ...     |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM