从SUM中删除否定结果

时间:2012-06-20 21:22:33

标签: php mysql

我使用以下代码对我的广告资源进行分组和汇总。对于我的部分库存,如果货物数量多于库存数量,我可能会得到负面结果,但我希望这些负数量项目不会显示在我的“库存页面”中。有没有办法删除数量等于或小于0的行?

-------------------------------------------------------------------
ProductID   |  Color  | Timestamp          |  Quantity  |  RowID  |
-------------------------------------------------------------------
25          |  Red    | 10/10/10 06:22:15  |  -250      |  1      |
-------------------------------------------------------------------
32          |  Green  | 10/10/10 06:23:45  |  100       |  2      |
-------------------------------------------------------------------

$query = "SELECT productid, color, SUM(Quantity) AS TotalQuantity FROM inventory GROUP BY productid, color";
$result = mysql_query($query) or die(mysql_error());

   // Table markup here
   echo '<tr><td>'. $row['productid'] . '</td><td>' . $row['color']. '</td><td>'. $row['TotalQuantity'];
   // Table markup here

1 个答案:

答案 0 :(得分:3)

$query = "Select * from 
  (SELECT productid, color, 
   SUM(Quantity) AS TotalQuantity FROM inventory 
   GROUP BY productid, color) AS alias 
where TotalQuantity  > 0";