2017-08-03 142 views
1

我怎麼能寫多個表e.g自定義查詢在Laravel

"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 
    "; 
在laravel 5.4

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 
    " 

查詢? DB ::語句可以運行這種類型的查詢?如果我在DB :: statement('')中編寫相同類型的查詢;

+1

爲什麼不利用模型和關係,並使用'Eloquent'來做這些查詢? –

+0

related(no dupe):https://stackoverflow.com/questions/28481321/how-to-do-a-left-outer-join-with-laravel –

+0

'SELECT items.id,items.name .... GROUP BY dispatch_detail.item_id'在較新的MySQL服務器上是無效的SQL ..請閱讀本文。 https://www.psce.com/en/blog/2012/05/15/mysql-mistakes-do-you-use-group-by-correctly/ –

回答

1
1 DB::table('dispatch_main') 
    2 ->innerJoin('dispatch_detail', 'dispatch_main.id', '=', 'dispatch_detail.id') 
    3 ->innerJoin('items', 'dispatch_detail.item_id', '=', 'items.id') 
    4 ->leftJoin('customers', 'dispatch_detail.customer', '=', 'customers.id') 
    5 ->whereIn('dispatch_main.entry_type', ['Purchase','Return','Confirmed']) 
    6 ->where('dispatch_main.to_', $location) 
    7 ->whereIn('items.for_customer', $types) 
    8 ->groupBy('dispatch_detail.item_id') 
    9 ->orderBy('items.id') 
10 ->get()->toArray(); 
11 
12 

嘗試此操作並始終避免編寫RAW查詢,直到您絕對擁有。

+0

@ParveenKumar但是,這次我想寫出原始查詢,請你告訴我我該怎麼做。我不想使用內部連接等Laravel功能 –

+0

這也很簡單,你只需要調用你正在發佈的查詢類型的靜態方法。像上面問題的查詢將是 –

+0

DB :: select(「select items.id,items.name,sum(qty)作爲qty從dispatch_main內部聯接dispatch_detail on dispatch_main.id = dispatch_detail.id內部聯接項目。 (?)中的dispatch_main.entry_type(dispatch_detail.customer = customers.id)中的('Purchase','Return','Confirmed')以及dispatch_main.to_ =?和items.for_customer中的id = dispatch_detail.item_id left outer join customers。通過dispatch_detail.item_id order by items.id「,[$ location,$ types]); –

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

希望這對你的工作!