我想在entotalitem和extotalitem之间进行减法运算,查询我使用的是从同一个表tbl_orders_data中检索数据。
我尝试了创建2个查询,第一个查询用于检索entotalitem,第二个查询用于检索extotalitem
$encheck = DB::table('tbl_orders_data')
->select('slot_id', DB::raw('sum(total_item) as entotalitem'))
->where('id_order_data', 'like', 'PBM' . '%')
->groupBy('slot_id')
->pluck('entotalitem');
$excheck = DB::table('tbl_orders_data')
->select('slot_id', DB::raw('sum(total_item) as extotalitem'))
->where('id_order_data', 'like', 'PBK' . '%')
->groupBy('slot_id')
->pluck('extotalitem');
$en = $encheck;
$ex = $excheck;
dd($en - $ex);
我只需要使用一个查询吗?还是应该像我尝试的那样进行2个查询? 请帮帮我,谢谢
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以在这里使用条件聚合:
$check = DB::table('tbl_orders_data') ->select('slot_id', DB::raw("sum(case when id_order_data like 'PBM%' then total_item else 0 end) - sum(case when id_order_data like 'PBK%' then total_item else 0 end) as totalitem")) ->groupBy('slot_id') ->pluck('totalitem');