[英]How to add where inside DB::raw() in laravel
我想獲得已completed
的訂單金額, rejected
的訂單計入 1 個查詢。 請在下面查看我的代碼。 如何在 DB::raw() 中添加位置或條件?
return DB::table('restaurants')
->join('orders', 'restaurants.id', 'orders.restaurant_id')
->select([
DB::raw('SUM(orders.total) WHERE status = 8 as completed_sum'),
DB::raw('COUNT(DISTINCT(orders.transaction_code)) WHERE status = 3 as rejected_count')
])
->get();
錯誤
SQLSTATE[42000]: Syntax error or access violation: 1064 你的 SQL 語法有錯誤;
更新
當我嘗試->toSql()
時,我得到了這個:
select `restaurant_name` as `restaurant`, SUM(total) WHERE status = 8 as completed_sum,
COUNT(DISTINCT(transaction_code)) WHERE status = 3 as rejected_count from `restuarants` inner join `orders` on
`restuarants`.`id` = `orders`.`restaurant_id` where `orders`.`date` between ? and ? group by `restuarants`.`id`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.