如何按月获得确切的总数?

时间:2016-05-26 06:46:16

标签: php mysql

我的数据项目有mySQL表。表行包括id,date,time和qty。

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

我想查询它,这样我就可以生成一个HTML表格,其中包含如下所示的报告:

enter image description here

我需要将所有数量和总数相加一个月。我怎么用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 } ?>

3 个答案:

答案 0 :(得分:2)

首先,我会使用类似于以下查询的内容:

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; ?>

以上是一些pseuodo代码,可以帮助您走上正确的轨道。祝你好运!

修改

为了帮助您,请使用您的代码。

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升级他的服务器。

答案 1 :(得分:-1)

试试这个,

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

答案 2 :(得分:-1)

尝试这样的事情:

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