【发布时间】:2015-09-05 06:50:56
【问题描述】:
DB::select(DB::raw( 'SELECT a.bill_no, a.account_id, a.bill_date, a.amount_paid,
b.transaction_code,b.amount from bill_det a left join
(select bill_no, transaction_code, sum(amount) as amount from payment_transactions
where status = "success" group by bill_no ) b
on a.bill_no = b.bill_no where a.amount_paid != b.amount order by b.bill_no'));
这是正常的查询。更改为 laravel 查询?。 我试过了。
$bill=DB::table('bill_det')->leftJoin('payment_transactions', 'bill_det.bill_no', '=', 'payment_transactions.bill_no')
->select('bill_det.bill_no','bill_det.account_id','bill_det.bill_date','bill_det.amount_paid',
'payment_transactions.transaction_code',DB::raw('sum(payment_transactions.amount) as amount'))
->where('payment_transactions.status','=','success')
->where('sum(payment_transactions.amount)','!=',DB::raw('bill_det.amount_paid'))
->groupBy('bill_det.bill_no')
->orderBy('bill_det.bill_no','desc');
我无法比较-> where('sum(payment_transactions.amount)','!=',DB::raw('bill_det.amount_paid'))
我这样用过->whereRaw('bill_det.amount_paid != sum(payment_transactions.amount)')
{"error":{"type":"Illuminate\Database\QueryException","message":"SQLSTATE[HY000]: 一般错误: 1111 无效使用组函数 (SQL: select count(*) as aggregate from (select '1' as row_count from
bill_detleft joinpayment_transactionsonbill_det.bill_no=payment_transactions.bill_nowherepayment_transactions.status= success and bill_det.amount_paid != sum (payment_transactions.amount) group bybill_det.bill_noorder bybill_det.bill_nodesc) count_row_table)"
【问题讨论】:
-
你试过了吗?
-
我试过了。问题是我无法比较总金额在哪里
-
需要使用 whereRaw 来比较 sum() 结果。
-
我使用了 whereRaw 但它给出了类似上面的错误。
-
我认为你应该使用“have”语句而不是“where”语句
标签: laravel-4