这是我在按日期为每个产品组选择 sum(amounts) 后得到的数组。当日期没有相应数量时,它显示为空。我需要将其显示为零。Array( [2019-05-16] => Array ( [0] => 499 ) [2019-05-17] => Array ( [0] => 1998 ) [2019-05-18] => Array ( [0] => 195 ) [2019-05-19] => Array ( [0] => 1194 [1] => 999 ))但是输出应该是这样的,有没有办法将空值设为 0Array( [2019-05-16] => Array ( [0] => 499 [1] => 0 ) [2019-05-17] => Array ( [0] => 1998 [1] => 0 ) [2019-05-18] => Array ( [0] => 195 [1] => 0 ) [2019-05-19] => Array ( [0] => 1194 [1] => 999 ))这是脚本foreach ($productid as $itemid) { $query1 = "select date, SUM(amount) as amount from app_product_details where date between '2019-05-14 00:00:00' and '2019-05-20 23:59:59' and appid = $itemid group by date"; $getquery1 = mysqli_query($conn,$query1); while($getcount1 = mysqli_fetch_assoc($getquery1)) { $grset1[] = $getcount1; } } foreach($grset1 as $r){$dates1[$r['date']][] = $r['amount'];}请帮助我达到输出。
1 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
与其尝试破解您的数组,不如更改您的查询,以便在当天没有销售任何给定产品时返回 0 值。这应该有效:
$query1 = "SELECT date, SUM(CASE WHEN appid = $itemid THEN amount ELSE 0 END) AS amount
FROM app_product_details
WHERE date BETWEEN '2019-05-14 00:00:00' AND '2019-05-20 23:59:59'
GROUP BY date";
- 1 回答
- 0 关注
- 137 浏览
添加回答
举报
0/150
提交
取消