您好我正在尝试将此mysql查询转换为laravel查询但仍然没有成功。你能帮忙把这个mysql查询转换成laravel查询吗?
Mysql查询如下
select *,SUM(pp.starting_balance) as total from (select `aa`.*,
GROUP_CONCAT( CONCAT(cc.purchasedescription," (",bb.quantity,")") SEPARATOR " , ") as bundle_item
from `composite_inventories` as aa
left join
`composite_has_inventories` as bb on `aa`.`id` = `bb`.`composite_inventory_id`
left join
`inventories` as cc on `bb`.`inventory_id` = `cc`.`id`
where
`aa`.`subscriber_id` = '2'
group by
`aa`.`id`) as tt left join `composite_has_warehouses` as pp on `tt`.`id` = `pp`.`composite_inventory_id` group by pp.composite_inventory_id
我尝试构建如下但不工作
$row = DB::table('composite_inventories')->select('composite_inventories.*',
DB::raw('SUM(composite_has_warehouses.starting_balance) as total')
DB::raw('GROUP_CONCAT( CONCAT(inventories.purchasedescription," (",composite_has_inventories.quantity,")") SEPARATOR " , ") as bundle_item')
)
->leftJoin('composite_has_inventories', 'composite_inventories.id', '=', 'composite_has_inventories.composite_inventory_id')
->leftJoin('inventories', function($join) {
$join->on('composite_has_inventories.inventory_id', '=', 'inventories.id');
})
->where('composite_inventories.subscriber_id',$subscriber_id)
->groupBy('composite_inventories.id')
->leftJoin('composite_has_warehouses', 'composite_inventories.id', '=', 'composite_has_warehouses.composite_inventory_id')
->get();
答案 0 :(得分:1)
您可以使用DB::select()
方法并将原始查询作为字符串放置:
DB::select("select *,SUM(pp.starting_balance) as total from (select `aa`.*,
DB::raw('GROUP_CONCAT( CONCAT(cc.purchasedescription,' (',bb.quantity,')') SEPARATOR ' , ') as bundle_item ')
from `composite_inventories` as aa
left join
`composite_has_inventories` as bb on `aa`.`id` = `bb`.`composite_inventory_id`
left join
`inventories` as cc on `bb`.`inventory_id` = `cc`.`id`
where
`aa`.`subscriber_id` = '2'
group by
`aa`.`id`) as tt left join `composite_has_warehouses` as pp on `tt`.`id` = `pp`.`composite_inventory_id` group by pp.composite_inventory_id");