larave -- leftJoin IFNULL 链表查询
--- 优惠卷主表 和 领取表的关联 目的查询有效优惠卷(还有可领取的数量)
$this->query = (new CouponRule())->newQuery();
if ($where) {
$this->query = $this->query->where($where);
}
$fields = array_merge($fields, [
\DB::raw("IFNULL(receive_count, 0) as coupon_log_count")
]);
if ($isInvalid != '-1' && $isInvalid === '0') {
// 获取有效优惠卷
$this->query = $this->query
->where('coupon_rule.is_invalid', 0)
->where(function ($query) {
return $query->where('coupon_rule.valid_time_end', '>', Carbon::now())->orWhere('coupon_rule.valid_days', '>', 0);
})
->where('coupon_rule.total', '>', \DB::raw("IFNULL(ds_d.receive_count, 0)"));
}
$this->query = $this->query->select($fields)
->leftJoin('coupon_data as d', function ($join) {
$join->on('coupon_rule.id', '=', 'd.coupon_id');
});
让每一滴智慧绘制成一条不归路!