查询在收货时将两个相同的产品打印为单个产品?

时间:2016-01-26 17:54:05

标签: php sql database

我正在开发php的销售点。我有两个不同的销售。我在第一次销售中销售了4部手机,并在第二次销售中销售了2部手机。以下查询在收据上打印了2件。但是我只想显示一件数量为6的物品(手机)?

<?php
$result = $db->query("SELECT * FROM stock_sales where date BETWEEN '$fromdate' AND '$todate' ");

while ($line = $db->fetchNextObject($result)) {
?>

<tr> 
<td><?php echo $line->stock_name; ?><
<td><?php echo $line->quantity; ?></td>
</tr>}

1 个答案:

答案 0 :(得分:0)

将您的查询修改为

SELECT stock_name, count(*), sum(quantity) AS sum_quantity FROM stock_sales where date BETWEEN '$fromdate' AND '$todate' group by stock_name