在Where子句中使用子查询的结果(Laravel)

时间:2014-12-07 18:59:07

标签: php mysql laravel laravel-4

我正在尝试在where子句中使用子查询的结果,但是我失败了

我正在使用laravel db class

这是我的代码

        DB::table('sales as SL')
        ->join('prsn as PR1','PR1.id_person','=','SL.customer_id')
        ->join('prsn as PR2','PR2.id_person','=','SL.investor_id')
        ->select('SL.customer_id' ,'SL.investor_id','SL.type_agd','SL.status','SL.id_agd','PR1.full_name_person as Buyer'
            ,'PR2.full_name_person as Seller','PR1.mobile1_person as Mobile',
            DB::raw('(select SUM(money_operation) from opers where agd_operation = SL.id_agd AND  ty IN ("3","4","5","6","7","8","9","10","12") AND status = 1 ) as Madfoo3'),
            DB::raw('(select SUM(money) from inst where agd_id = SL.id_agd AND date <= "'.DT::Today('Ymd').'" AND type != "D") as Matloob'),
            DB::raw('(select (Matloob - Madfoo3 ))   Mutakher' ))
        ->whereRaw('(Mutakher > 0)')
        ->paginate();

出现此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Mutakher' in 'where clause' (SQL: select count(*) as aggregate from `sales` as `SL` inner join `prsn` as `PR1` on `PR1`.`id_person` = `SL`.`customer_id` inner join `prsn` as `PR2` on `PR2`.`id_person` = `SL`.`investor_id` where (Mutakher > 0))
/var/www/Taqseet/vendor/laravel/framework/src/Illuminate/Database/Connection.php#625

如果“Mutakher”&gt;我想提出一个条件0给我原始的。

由于

1 个答案:

答案 0 :(得分:0)

我解决了这个问题

    DB::table('sales as SL')
    ->join('prsn as PR1','PR1.id_person','=','SL.customer_id')
    ->join('prsn as PR2','PR2.id_person','=','SL.investor_id')
    ->select('SL.customer_id' ,'SL.investor_id','SL.type_agd','SL.status','SL.id_agd','PR1.full_name_person as Buyer'
        ,'PR2.full_name_person as Seller','PR1.mobile1_person as Mobile',
        DB::raw('(select SUM(money_operation) from opers where agd_operation = SL.id_agd AND  ty IN ("3","4","5","6","7","8","9","10","12") AND status = 1 ) as Madfoo3'),
        DB::raw('(select SUM(money) from inst where agd_id = SL.id_agd AND date <= "'.DT::Today('Ymd').'" AND type != "D") as Matloob'),
        DB::raw('(select (Matloob - Madfoo3 ))   Mutakher' ))
    ->whereRaw('((Matloob - Madfoo3 ) > 0)')
    ->paginate();