簡體   English   中英

如何按月獲得確切的總數?

[英]how to get the exact total by month?

我的數據項目有 mySQL 表。每個表行都包括 ID、日期、時間和數量。

id   | date | time |  qty
-------------------------

我想查詢它,以便我可以生成一個 HTML 表,其中包含一個如下所示的報告:

在此處輸入圖片說明

我需要將所有數量和總數相加,為期一個月。 我將如何做到這一點使用 php?

這是我嘗試過的代碼...

 <table width="522" border="0" align="center" bgcolor="#FF9900"> <tr bgcolor="#FF9900"> <td bgcolor="#333333" colspan="7" bgcolor="#FFFFFF" align="center"><strong>List of Shopping</strong></td> </tr> <tr bgcolor="#FF9900"> <td align="center"><font face="Lato" color="#D30CAB"><strong>MONTH</a></strong></font></td> <td align="center"><font face="Lato" color="#D30CAB"><strong>QUANTITY</strong></font></td> <td align="center"><font face="Lato" color="#D30CAB"><strong>DELETE</strong></font></td> </tr> <?php include('connection.php'); $user_query=mysql_query("SELECT * FROM `buy` WHERE 1"); while($user_rows=mysql_fetch_array($user_query)) { ?> <tr> <td align="center" bgcolor="#FF9900"><?php echo $user_rows['date'] ; ?></td> <td align="center" bgcolor="#FF9900"><?php echo $user_rows['qty'] ; ?></td> <td align="center" bgcolor="#FF9900"><a href="delete2.php<?php echo '?id='.$user_rows['id']; ?>">Delete</a></td> </tr> <?php }?> </table> </main> <?php $result = mysql_query("SELECT sum(qty) FROM buy") or die(mysql_error()); while ($user_rows = mysql_fetch_array($result)){ ?> <?php } ?>

首先,我將使用類似於以下查詢的內容:

SELECT
MONTH(date) as month,
SUM(qty) as total
FROM table
WHERE MONTH(date) = 5
GROUP BY MONTH(date)
ORDER BY date DESC

然后,使用 foreach 或 while(取決於您執行查詢的方式)循環遍歷帶有結果的數組並構建您的表。

<?php foreach ($results as $result): ?>
<tr>
    <td><?= $result['month']; ?></td>
    <td><?= $result['total']; ?></td>
</tr>
<?php endforeach; ?>

以上是一些偽代碼,可以幫助您走上正軌。 祝你好運!

編輯

為了幫助您,這里有一個使用您的代碼的示例。

include('connection.php');
$query = mysql_query("SELECT
    MONTH(date) as month,
    SUM(qty) as total
    FROM buy
    WHERE MONTH(date) = 5
    GROUP BY MONTH(date)
    ORDER BY date DESC
") or die("Error: ".mysql_error());

$resource = array();
while ($record = mysql_fetch_assoc($query)) {
    $results[] = $record;
}

你的桌子:

<table width="522" border="0" align="center" bgcolor="#FF9900">
    <tr bgcolor="#FF9900">
        <td bgcolor="#333333" colspan="7" bgcolor="#FFFFFF" align="center"><strong>List of Shopping</strong></td>
    </tr>
    <tr bgcolor="#FF9900">
        <td  align="center"><font face="Lato"  color="#D30CAB"><strong>MONTH</a></strong></font></td>
        <td align="center"><font face="Lato"  color="#D30CAB"><strong>QUANTITY</strong></font></td>
        <td align="center"><font face="Lato"  color="#D30CAB"><strong>DELETE</strong></font></td>
    </tr>
    <?php foreach ($results as $result): ?>
    <tr>
        <?php $date = DateTime::createFromFormat('Y-m', '2016-'.$result['month']); ?>
        <td align="center" bgcolor="#FF9900"><?= $date->format('M'); ?></td>
        <td align="center" bgcolor="#FF9900"><?= $result['total']; ?></td>
        <td align="center" bgcolor="#FF9900">Deleting will not work since it is a grouped result. You ccannot delete by id.</td>
    </tr>
    <?php endforeach; ?>
</table>

讀者注意:我使用了mysql_*因為 OP 在他的代碼中使用了它。 我不能簡單地要求 OP 在回答之前升級他的服務器。

嘗試這個,

    SELECT
            MONTH(`date`) AS `MONTH`, SUM(qty) AS `TOTAL`
    FROM 
            < tableName >
    GROUP BY 
            MONTH(`date`)

嘗試這樣的事情:

select SUM(qty) as total, id, MONTH(date) 
from your_table
group by MONTH(date)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM