Write SQL sum with join and group by in Laravel? - php

I wish to write a query from SQL Server to laravel.
I want to write like this SQL:
select SUM(TRANSTKD.TRD_U_PRC) as prctotal from DOCINFO
inner join [TRANSTKH] on [DOCINFO].[DI_KEY] = [TRANSTKH].[TRH_DI]
inner join [SKUMASTER]
inner join [TRANSTKD]
on [TRANSTKD].[TRD_SKU] = [SKUMASTER].[SKU_KEY]
on [TRANSTKH].[TRH_KEY] = [TRANSTKD].[TRD_TRH]
where [DOCINFO].[DI_KEY] = 148978
GROUP BY [SKUMASTER].[SKU_CODE];

Try this below laravel query
DB::table('DOCINFO')
->join('TRANSTKH', 'DOCINFO.DI_KEY', '=', 'TRANSTKH.TRH_DI')
->join('TRANSTKD', 'TRANSTKH.TRH_KEY', '=', 'TRANSTKD.TRD_TRH')
->join('SKUMASTER', 'TRANSTKD.TRD_SKU', '=', 'SKUMASTER.SKU_KEY')
->select(DB::raw('SUM(TRANSTKD.TRD_U_PRC) as prctotal'))
->where('DOCINFO.DI_KEY',148978)
->groupBy('SKUMASTER.SKU_CODE')
->get();
Let me know how it helps you!

Related

raw query join left with select to laravel 8 query

i'm using raw query on my laravel function, and i wanna make it into laravel query builder, but i really have no idea how to do it, i've read laravel documentary about advanced join clause or subquery joins, but still cant figuring it out how to convert it
raw query :
$buku = DB::select(
DB::raw('
SELECT buku.*, kategory, tag
FROM buku
LEFT JOIN kategori_buku ON buku.id_kategori = kategori_buku.id
LEFT JOIN detail_buku_tag ON buku.id = detail_buku_tag.id_buku
LEFT JOIN (SELECT tag_buku.id, GROUP_CONCAT(tag) AS tag FROM tag_buku) AS tag_buku ON tag_buku.id = detail_buku_tag.id_tag
GROUP BY buku.id')
);
there some sql chaining issue. #Ramiz Kongulov forget to split group by clause try this.
$buku = \DB::table('buku')
->select(['buku.*','kategory.*', 'tag.*'])
->leftJoin('kategori_buku', 'kategori_buku.id', '=', 'buku.id_kategori')
->leftJoin('detail_buku_tag', 'detail_buku_tag.id_buku', '=', 'buku.id')
->leftJoin(\DB::raw('(SELECT tag_buku.id, GROUP_CONCAT(tag) AS tag FROM tag_buku) AS tag_buku'),
'tag_buku.id', '=', 'detail_buku_tag.id_tag')
->groupBy('buku.id')
->get();
try this
$buku = \DB::table('buku')
->select([
'buku.*',
'kategory.*',
'tag.*'
])
->leftJoin('kategori_buku', 'kategori_buku.id', '=', 'buku.id_kategori')
->leftJoin('detail_buku_tag', 'detail_buku_tag.id_buku', '=', 'buku.id')
->leftJoin(DB::raw('SELECT tag_buku.id, GROUP_CONCAT(tag) AS tag FROM tag_buku) AS tag_buku ON tag_buku.id = detail_buku_tag.id_tag GROUP BY buku.id'))
->get();

This row query working properly please help to change it eloquent query

SELECT `foduu_listing`.`id`,`foduu_listing_filedetail`.`primary`, `foduu_listing`.`name`,`foduu_listing`.`filemanager_id`,`foduu_filemanager`.`filepath`,`foduu_detail_orders`.`listing_id`, COUNT(`foduu_detail_orders`.`listing_id`) AS count,SUM(`foduu_detail_orders`.`total`) AS total
FROM foduu_listing
left join `foduu_detail_orders` on `foduu_listing`.`id` = `foduu_detail_orders`.`listing_id`
left join `foduu_listing_filedetail` on `foduu_listing`.`id` = `foduu_listing_filedetail`.`listing_id`
left join `foduu_filemanager` on `foduu_listing`.`filemanager_id` = `foduu_filemanager`.`id`
where `foduu_detail_orders`.`listing_id` = 593
I would do it like this:
DB::table('foduu_listing')->select('foduu_listing.id`,foduu_listing_filedetail.primary, foduu_listing.name,foduu_listing.filemanager_id,foduu_filemanager.filepath,foduu_detail_orders.listing_id')
->leftJoin('foduu_detail_orders', 'foduu_listing.id', '=', 'foduu_detail_orders.listing_id')
->leftJoin('foduu_listing_filedetail', 'foduu_listing.id', '=', 'foduu_listing_filedetail.listing_id')
->leftJoin('foduu_filemanager', 'foduu_listing.filemanager_id', '=', 'foduu_filemanager.id')
->selectRaw('COUNT(`foduu_detail_orders`.`listing_id`) AS count')
->selectRaw('SUM(`foduu_detail_orders`.`total`) AS total')
->where('foduu_detail_orders.listing_id', 593)
->get();
If instead of calling get at the end you call toSql you can check the query generated:
select `foduu_listing`.`id``,foduu_listing_filedetail`.`primary, foduu_listing`.`name,foduu_listing`.`filemanager_id,foduu_filemanager`.`filepath,foduu_detail_orders`.`listing_id`, COUNT(`foduu_detail_orders`.`listing_id`) AS count, SUM(`foduu_detail_orders`.`total`) AS total from `foduu_listing` left join `foduu_detail_orders` on `foduu_listing`.`id` = `foduu_detail_orders`.`listing_id` left join `foduu_listing_filedetail` on `foduu_listing`.`id` = `foduu_listing_filedetail`.`listing_id` left join `foduu_filemanager` on `foduu_listing`.`filemanager_id` = `foduu_filemanager`.`id` where `foduu_detail_orders`.`listing_id` = ?
However you are not making use of Laravel tools if you just translate your queries from raw SQL to Eloquent like this, you should define your models and your relationships properly and then make use of that for querying your data.

Laravel query builder with joins

I am blocked in the conversion of my raw sql query to laravel query builder. Can someone give me a hand on the issue?
select p.name, p.`type`, p.`status`, p.state, ps1.* from pos_session ps1 right join pos p on ps1.pos_id = p.id where ps1.id = (select max(ps0.id) from pos_session ps0 where ps0.pos_id = ps1.pos_id) or p.site_id = '2' order by ps1.pos_id;
Try following query:
DB::table('pos_session AS ps1')
->select('p.name','p.type','p.status','p.state','ps1.*')
->rightJoin('pos AS p','ps1.pos_id','=','p.id')
->where('ps1.id','=',function($query){
$query->select(DB::raw('MAX(ps0.id)'))
->from('pos_session AS ps0')
->where('ps0.pos_id','=','ps1.pos_id');
})
->orWhere('p.site_id','=',2)
->orderBy('ps1.pos_id')
->get();
you can achieve the results through join as well:
DB::table('pos_session AS ps1')
->select('p.name','p.type','p.status','p.state','ps1.*')
->rightJoin('pos AS p','ps1.pos_id','=','p.id')
->join('pos_session AS ps0', 'ps0.pos_id', '=', 'ps1.pos_id')
->where('ps1.id','=',DB::raw('MAX(ps0.id)'))
->orWhere('p.site_id','=',2)
->orderBy('ps1.pos_id')
->get();
Hope it helped :)

laravel DB query select multiple tables without joining

I have a Raw SQL query here and what to know how to write this Raw query in laravel query builder
Here is the Raw Query :
SELECT tbl_stock.* , series.*, series_size.*, tbl_size.* FROM
tbl_stock , series, series_size, tbl_size
WHERE
tbl_stock.series_id = series.series_id AND
tbl_stock.size_id = series_size.size_id AND
tbl_stock.size_id = tbl_size.size_id
I have Tried DB Query builder laravel but getting exception
$stock_info = DB::table(['tbl_stock', 'shades'])->get();
This gives me this
Array to String Conversion Exception
THANKS IN ADVANCE
First of all, you should replace your archaic join syntax with modern syntax:
SELECT
t1.*,
t2.*,
t3.*,
t4.*
FROM tbl_stock t1
INNER JOIN series t2
ON t1.series_id = t2.series_id
INNER JOIN series_size t3
ON t1.size_id = t3.size_id
INNER JOIN tbl_size t4
ON t1.size_id = t4.size_id;
Then, use Laravel's query builder syntax to build your query in PHP code:
$stocks = DB::table('tbl_stock')
->join('series', 'tbl_stock.series_id', '=', 'series.series_id')
->join('series_size', 'tbl_stock.size_id', '=', 'series_size.size_id')
->join('tbl_size', 'tbl_stock.size_id', '=', 'tbl_size.size_id')
->select('tbl_stock.*', 'series.*', 'series_size.*', 'tbl_size.*')
->get();

Using And Clause in Laravel Query Bulider

I have the following query:
SELECT
customers.CustCompanyName,
addresses.AddressLine1,
orders.*
FROM orders
INNER JOIN customers
ON orders.CustID = customers.CustID
INNER JOIN customer_addresses
ON customer_addresses.CustID = customers.CustID
INNER JOIN addresses
ON customer_addresses.AddressID = addresses.AddressID
AND orders.CustAddID = customer_addresses.CustAd
How do I incorporate the AND clause in using laravel Query Builder:
$orders = DB::table('orders')
->join('customers', 'orders.CustID', '=', 'customers.CustID')
->join('customer_addresses','customer_addresses.CustID', '=' , 'customers.CustID' )
->join('addresses','customer_addresses.AddressID','=','addresses.AddressID')->AND
I can't find any working examples using the AND clause.
For:
INNER JOIN addresses
ON customer_addresses.AddressID = addresses.AddressID
AND orders.CustAddID = customer_addresses.CustAd
you should use:
->join('addresses', function($join) {
$join->on('customer_addresses.AddressID', '=', 'addresses.AddressID')
->whereRaw('orders.CustAddID = customer_addresses.CustAd');
});
You can also look at Joins in Query Builder for details

Categories