MySQL聚合是处理空值_MYSQL:在日期范围内聚合订单,其值为空值

bd96500e110b49cbb3cd949968f18be7.png

Im trying to graph the sum of orders occuring for a particular date for the last 7 days but am having a problem when their aren't any orders for a particular date as the query isn't a value nor a date and therefore returning an array less than 7.

SELECT IFNULL(COUNT(*),0) as purchase_count, DATE(tb_order.order_date) as order_date

FROM tb_order_attendee_info

JOIN tb_order on tb_order.order_id = tb_order_attendee_info.order_id

JOIN tb_events on tb_events.event_id = tb_order.event_id

WHERE tb_order_attendee_info.refunded='N' AND order_date between DATE_SUB(now(), interval 10 day) and now()

GROUP BY DATE(tb_order.order_date)";

Ive been trying to work out how i can get it to return the array with zero values for purchase count and the incremented date, unfortunately its only returning a date when the count is greater than 0.

解决方案

You are trying to introduce result rows where there are none in the data. This is just not possible (without using a Stored Procedure). You cannot iterate over the dates as, so to say, there are no for loops in SQL.

The closest you can get is having/creating a (temporary) table of all the dates you want to have in the result and then JOIN to this table.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值