gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:20:32 25 4
gpt4 key购买 nike

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

我正在使用 laravel 数据库类

这是我的代码

        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” > 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();

关于php - 在Where子句中使用子查询的结果(Laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27346562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com