0

假设我有以下查询,它给了我下载和兑换的优惠券数量,按日期分组:[编辑](我无法修改此查询)[/编辑]

$sql = "SELECT DATE(datetime) ,
                           SUM(CASE WHEN datetime!=''  THEN 1 ELSE 0 END) As downloaded ,
                           SUM(CASE WHEN used = 1 AND DATE(datetime) != '' THEN 1 ELSE 0 END) AS redeemed

                     FROM Promotion_Redeem
                     WHERE datetime BETWEEN '2013-10-01' AND '2013-10-04'
                     GROUP BY DATE(datetime)";

我怎样才能得到下载的总和和兑换的总和?这应该在 $sql 内,某处......下面是这个查询的结果:

row number 1
array (size=3)
0 => string '2013-10-01' (length=10)
1 => string '126' (length=3)
2 => string '11' (length=2)
row number 2
array (size=3)
0 => string '2013-10-02' (length=10)
1 => string '106' (length=3)
2 => string '5' (length=1)
row number 3
array (size=3)
0 => string '2013-10-03' (length=10)
1 => string '228' (length=3)
2 => string '12' (length=2)
row number 4
array (size=3)
0 => string '2013-10-04' (length=10)
1 => string '149' (length=3)
2 => string '9' (length=1)

[编辑]你可以看到我得到 4 行,每行都有一个数组...例如,我想要每个数组中第三个字段的总和...在我的示例中,这将等于 37 (这意味着,兑换了 37 张优惠券)[/EDIT] 使用此数据结构后,我得到了这个数据结构:

while ($row = mysql_fetch_row($result)) {
            echo "row number ".++$i;
            var_dump($row);

        }

提前致谢

4

1 回答 1

1

像这样修改你的 php 循环:

$downloaded=0;
$redeemed=0;
while ($row = mysql_fetch_row($result)) {
        echo "row number ".++$i;
        $downloaded+=$row[1];
        $redeemed+=$row[2];
        var_dump($row);
    }
echo "Downloaded: $downloaded<br>";    
echo "Redeemed: $redeemed<br>";
于 2013-10-08T19:00:38.280 回答