简体   繁体   中英

Mysql loop through database based on timestamp?

In my database I store orders as entryDate based on Unix timestamp. I would like to make a simple HTML chart that displays the number of orders of a given day. But this is thougher that it sounds (for me atleast).

How do I loop through my database based on an unix timestamp? How do I then get the total number of orders of that day? The furtest I got is:

SELECT COUNT(id) 
FROM customers_orders 
WHERE entryDate >= NOW() - INTERVAL 7 DAY
AND status != 99 AND totalprice > 0 AND importId = 0

For orders of TODAY I use:

SELECT COUNT(id) FROM customers_orders WHERE DATE_FORMAT(FROM_UNIXTIME(`entryDate`), '%Y-%m-%d') = CURDATE() AND status != 99

The end result should be something simple like:

<table>
<tr>
    <td><?= $objStats->numMonday; ?></td>
    <td>Monday</td>
</tr>
<tr>
    <td><?= $objStats->numTuesday; ?></td>
    <td>Tuesday</td>
</tr>
<tr>
    <td><?= $objStats->numWednesday; ?></td>
    <td>Wednesday</td>
</tr>
<tr>
    <td><?= $objStats->numThursday; ?></td>
    <td>Thursday</td>
</tr>
<tr>
    <td><?= $objStats->numFriday; ?></td>
    <td>Friday</td>
</tr>
<tr>
    <td><?= $objStats->numSaturday; ?></td>
    <td>Saturday</td>
</tr>
<tr>
    <td><?= $objStats->numSunday; ?></td>
    <td>Sunday</td>
</tr>                                                                                                                           
</table>

DATE(entryDate) gives you the YYYY MM DD part of your timestamp, group your query with that.

SELECT COUNT(id), DATE(entryDate)
FROM customers_orders 
WHERE entryDate >= NOW() - INTERVAL 7 DAY
AND status != 99 AND totalprice > 0 AND importId = 0
GROUP BY DATE(entryDate)

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