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
Related
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();
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!
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.
I have been this SQL working fine, but trying to convert it to Eloquent format, it keeps returning a wrong, SQL entirely which flags error.
Coding with Laravel 5.5
Select arm_articles.article_topic, arm_articles.id, arm_articles.article_id,
COUNT(arm_article_views.view_article_id) AS TotalViews,
COUNT( arm_article_likes.liked_article_id) AS TotalLikes,
COUNT( arm_article_comments.comment_article_id) AS TotalComments
FROM arm_articles
LEFT JOIN arm_article_views ON arm_articles.article_id = arm_article_views.view_article_id
LEFT JOIN arm_article_likes ON arm_articles.article_id = arm_article_likes.liked_article_id
LEFT JOIN arm_article_comments ON arm_articles.article_id = arm_article_comments.comment_article_id
GROUP BY arm_articles.article_id
ORDER BY TotalLikes, TotalLikes, TotalComments ASC`
HOW I MADE ELOQUENT QUERY BUILDER Not working though:
return Datatables::of(PostModel::leftJoin('arm_article_views', 'arm_article_views.view_article_id', '=', 'arm_articles.article_id')
->leftJoin('arm_article_likes','arm_article_likes.liked_article_id', '=', 'arm_articles.article_id')
->leftJoin('arm_article_comments', 'arm_article_comments.comment_article_id','=','arm_articles.article_id')
->selectRaw(
'arm_articles.*,
count(arm_article_views.view_article_id) AS ViewCount'
)
->groupBy('arm_articles.article_id')->orderBy('ViewCount','DESC')
->where('arm_articles.article_contributor_id','=',$contributor_id)
->getQuery())->make(true);
Any hint would be appreciated
Try with this:
DB::table('arm_articles')
->leftJoin('arm_article_views', 'arm_articles.article_id', '=', 'arm_article_views.view_article_id')
->leftJoin('arm_article_likes', 'arm_articles.article_id', '=', 'arm_article_likes.liked_article_id')
->leftJoin('arm_article_comments', 'arm_articles.article_id', '=', 'arm_article_comments.comment_article_id')
->groupBy('arm_articles.article_id', 'article_topic')
->select(
DB::raw('count(arm_article_views.view_article_id) as TotalViews'),
DB::raw('count(arm_article_likes.liked_article_id) as TotalLikes'),
DB::raw('count(arm_article_comments.comment_article_id) as TotalComments'),
'arm_articles.article_id',
'article_topic'
)->get();
I have this query that successfully run on mysql workbench:
SELECT
any_value(u.username),
any_value(b.name),
any_value(gc.visit_cycle_id),
any_value(gc.group_number),
any_value(dc.total_customer),
count(*) as total_day
FROM
trackgobackenddb.group_cycles gc
LEFT JOIN (
SELECT
any_value(dc.group_cycle_id) as group_cycle_id,
count(*) as total_customer
FROM
trackgobackenddb.destination_cycles dc
GROUP BY dc.group_cycle_id
) dc ON dc.group_cycle_id = gc.id
LEFT JOIN visit_cycles vc ON gc.visit_cycle_id = vc.id
LEFT JOIN users u ON vc.user_id = u.id
LEFT JOIN branches b ON vc.branch_id = b.id
GROUP BY gc.visit_cycle_id, gc.group_number;
How to convert that query so I can use laravel eloquent or query builder?
You can use DB::select to run complex raw SQL
$sql = "";
$data = DB::select($sql);
Here's my untested attempt using the query builder:
DB::table('group_cycles AS gc')
->leftJoin('destination_cycles AS dc', function ($join) {
$join->on('dc.group_cycle_id', '=', 'gc.id')
->select(['dc.group_cycle_id AS group_cycle_id', DB::raw('select count(*) AS total_customer')]);
})
->leftJoin('visit_cycles AS vc', function ($join) {
$join->on('gc.visit_cycle_id', '=', 'vc.id');
})
->leftJoin('users AS u', function ($join) {
$join->on('vc.user_id', '=', 'u.id');
})
->leftJoin('branches AS b', function ($join) {
$join->on('vc.branch_id', '=', 'b.id');
})
->groupBy('gc.visit_cycle_id', 'gc.group_number')
->get();