[英]Retrieving specific data from sql query
Let's say I have the following query, wich gives me the amount of coupons downloaded and redeemed, grouped by date: [EDIT](I can't modify this query)[/EDIT] 假设我有以下查询,根据日期,这会给我下载和兑换的优待券数量:[EDIT](我无法修改此查询)[/ EDIT]
$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)";
How can I get the sum of downloaded and the sum of redeemed? 如何获得已下载和已兑换的总和? this should be within $sql, somewhere... below is the result for this query:
这应该在$ 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)
[EDIT]bove you can see I get 4 rows, each one whith an array... I want, for instance, the sum of the third field from each of these arrays... In my example, this would equals to 37 (that means, 37 coupons redeemed)[/EDIT] I got this data structure after using this: [EDIT]您可以看到我得到4行,每行有一个数组...例如,我想要这些数组中每个数组的第三个字段的总和...在我的示例中,这等于37(也就是说,兑换了37张优惠券)[/ EDIT]使用以下代码后,我得到了以下数据结构:
while ($row = mysql_fetch_row($result)) {
echo "row number ".++$i;
var_dump($row);
}
Thanks in advance 提前致谢
Modify your php loop like so: 像这样修改您的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>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.