请帮助,如何将这些代码传输到查询构建器?
SELECT *,
SUM(used_count),COUNT(code)
FROM promo_codes pc
LEFT JOIN promo_code_user pcu on pc.id=pcu.`promo_code_id`
LEFT JOIN promo_code_types pct on pc.`promo_code_type_id`=pct.id
WHERE (pc.expiry_date >= CURDATE() or pc.expiry_date is NULL) and
(pct.expiry_date >= CURDATE() or pct.expiry_date is null)
GROUP BY code
HAVING ( (sum(used_count) < global_max_uses or sum(used_count) is null) and
( count(code) < max_users or count(code) is null) ) ;
谢谢
答案 0 :(得分:0)
我无法测试查询,所以我在运行中尝试了一些东西。
$promo = DB::table('promo_codes')
->select(‘*’,DB::raw('SUM(used_count),COUNT(code)'))
->leftjoin('promo_code_user','promo_codes.id','=','promo_code_user.promo_code_id')
->leftjoin('promo_code_types','promo_codes.promo_code_type_id','=','promo_code_types.id')
->where(function ($query) {
$query->where('promo_codes. expiry_date', '>=',DB::raw('curdate()'))
->orWhereNull('promo_codes.expiry_date');
})
->where(function ($query) {
$query->where('promo_code_types.expiry_date', '>=', DB::raw('curdate()'))
->orWhereNull('promo_code_types.expiry_date');
})
->groupBy('code')
->havingRaw((sum(used_count) < global_max_uses or sum(used_count) is null) and
( count(code) < max_users or count(code) is null)
)->get();
如果您发现任何错误,请在此处发布,以便为您提供帮助
答案 1 :(得分:0)
我对你有个好主意,你可以简单地在laravel中使用这个查询而不进行任何转换,请看这个
$results = DB::select( DB::raw("Write Your Query Here ") );
//this will return an array of results , and you can also add parameters to the query as you like