【问题标题】:laravel query builder from normal query来自普通查询的 laravel 查询生成器
【发布时间】: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_det left join payment_transactions on bill_det.bill_no = payment_transactions.bill_no where payment_transactions.status = success and bill_det.amount_paid != sum (payment_transactions.amount) group by bill_det.bill_no order by bill_det.bill_no desc) count_row_table)"

【问题讨论】:

  • 你试过了吗?
  • 我试过了。问题是我无法比较总金额在哪里
  • 需要使用 whereRaw 来比较 sum() 结果。
  • 我使用了 whereRaw 但它给出了类似上面的错误。
  • 我认为你应该使用“have”语句而不是“where”语句

标签: laravel-4


【解决方案1】:
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 Query::

$query = \Illuminate\Support\Facades\DB::table('bill_det')
            ->select('a.bill_no', 'a.account_id', 'a.bill_date', 'a.amount_paid', 'b.transaction_code', 'b.amount')
            ->leftJoin(DB::raw('(select bill_no, transaction_code, sum(amount) as amount from payment_transactions 
                            where status = "success" group by bill_no) b'), function($join) {
                $join->on('a.bill_no', '=', 'b.bill_no');
            })
            ->where('a.amount_paid','<>', 'b.amount')
             ->orderBy('b.bill_no')
            ->get();

如果您想知道如何在 where 中使用原始表达式,请使用:

$query->whereRaw(DB::raw('(your expression!!)'));

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2021-12-03
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多