Where did you get stuck?
May 3, 2020
28
Level 4
How to merge three query in one
I have three query, and i want help to merge to have one, and order by Indate and Outdate as date
$item_load_list_cur_month=DB::table('products')
->select('loadings.qty','loadings.Outdate','products.name')
->join('loadings', 'loadings.pid' ,'=','products.id')
->whereYear('loadings.Outdate', Carbon::now()->year)
->whereMonth('loadings.Outdate', Carbon::now()->month)
->where('loadings.pid','=', 6)
// ->OrderBy('loadings.Outdate','ASC')
->get();
$item_stock_list_cur_month=DB::table('products')
->select('stocks.qty','stocks.Indate','products.name')
->join('stocks', 'stocks.pid' ,'=','products.id')
->whereYear('stocks.Indate', Carbon::now()->year)
->whereMonth('stocks.Indate', Carbon::now()->month)
->where('stocks.pid','=', 6)
->get();
dd($item_stock_list_cur_month);
$open_balance_prev=DB::select(DB::raw(" SELECT products.id,products.name,
(
(products.open_stock)
+(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and
MONTH(stocks.Indate) <= MONTH(CURRENT_DATE())-1)
- (select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and
MONTH(loadings.Outdate) <= MONTH(CURRENT_DATE())-1)
)
as open_balance
from products where products.id= 6"));
Level 61
I'm not sure if you've heard what I've said. So there is no point to join multiple queries into one when the data you get from those queries is not usable in foreach loop which you are using to display the data. Use separate queries and display the data where needed. Use that total data to subtract from when looping in order to display the correct status after that change.
Please or to participate in this conversation.