我try 在单个查询中使用多个Count和Sum命令. 然而,当我使用以下内容时,会发生什么情况:
SUM(amount AND type = "dispute") as charge
个
它实际上计算的是记录的数量,而不是总数. 我想要做的是多次使用带有WHERE子句的SUM,例如:
`$query = DB::table('cash_table')
->where('transaction_created', '>=', Carbon::now()->startOfWeek())
->select(DB::raw('
SUM(amount) as transaction_amount,
COUNT(amount) as transaction_count,
SUM(amount) WHERE type = "dispute" as charge_amount
COUNT(amount) WHERE type = "dispute" as charge_count
SUM(loan_amount) WHERE type = "loan" as loan_amount
COUNT(loan_amount) WHERE type = "loan" as loan_count
'))
->groupBy('transaction_created')
->get();`
前两个"TRANSACTION_AMOUNT&;TRANSACTION COUNT"运行良好,因为没有WHERE子句",而另一个则不行.