如何为多个表编写查询,例如
"select items.id, items.name, sum(qty) as qty from dispatch_main
inner join dispatch_detail on dispatch_main.id = dispatch_detail.id
inner join items on items.id = dispatch_detail.item_id
left outer join customers on dispatch_detail.customer = customers.id
where dispatch_main.entry_type in ('Purchase','Return','Confirmed') and
dispatch_main.to_=$location and items.for_customer in ($types)
group by dispatch_detail.item_id
order by items.id
";
OR
"select items.id, items.name, sum(qty) as qty from dispatch_main
inner join dispatch_detail on dispatch_main.id = dispatch_detail.id
inner join items on items.id = dispatch_detail.item_id
left outer join customers on dispatch_detail.customer = customers.id
where dispatch_main.entry_type in ('Dispatch','Confirmed') and
dispatch_main.from_=$location and items.for_customer in ($types)
group by dispatch_detail.item_id
order by items.id
"
在laravel 5.4? DB :: statement可以运行这种类型的查询吗?如果我在DB :: statement('')中编写相同类型的查询;
答案 0 :(得分:1)
<script async defer type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&callback=initMap"></script>
<div id="map" style="width:500px;height:150px"></div>
试试这个并且总是避免写RAW查询,直到你完全没有。
答案 1 :(得分:0)
你应该试试这个:
DB::table('dispatch_main')
->select('items.id',DB::raw('SUM(dispatch_main.qty) as qty'))
->join('dispatch_detail', 'dispatch_main.id', '=', 'dispatch_detail.id')
->join('items', 'dispatch_detail.item_id', '=', 'items.id')
->join('customers', 'dispatch_detail.customer', '=', 'customers.id')
->whereIn('dispatch_main.entry_type', ['Purchase','Return','Confirmed'])
->where('dispatch_main.to_', $location)
->whereIn('items.for_customer', $types)
->groupBy('dispatch_detail.item_id')
->orderBy('items.id')
->get()
->toArray();
希望这对你有用!!!