How to write a subquery in Laravel query builder - php

i have an mssql query that looks like this:
SELECT * FROM (
SELECT a.Client, ad.Kto, a.Address, a.Matchcode, a.Name1, a.country, a.ZIP, a.Street FROM [HeadQuarter].[dbo].[Addresses] a
INNER JOIN [HeadQuarter].[dbo].[AddressesDetails] ad ON (a.Client = ad.Client AND
a.Address = ad.Address)
WHERE ad.Active <> 0 AND
(a.USER_emailactive = 0 OR a.USER_emailactive IS NULL)
) client_id
WHERE (a.Country = 'AT' AND a.ZIP BETWEEN '0000' AND '5000')
i converted the inner select into Laravel query builder
\DB::connection('sqlsrv')
->table('[HeadQuarter].[dbo].[Addresses]')
->join([HeadQuarter].[dbo].[AddressesDetails], function($join){
$join->on('[HeadQuarter].[dbo].[Addresses].Client', '=', '[HeadQuarter].[dbo].[AddressesDetails].Client')
->on('[HeadQuarter].[dbo].[Addresses].Address', '=', '[HeadQuarter].[dbo].[AddressesDetails].Address')
})
->select('[HeadQuarter].[dbo].[Addresses].Client, [HeadQuarter].[dbo].[AddressesDetails].Kto,....')
->where('[HeadQuarter].[dbo].[AddressesDetails].Active', '<>', '0')
->whereRaw('(a.USER_emailactive = 0 OR a.USER_emailactive IS NULL)')
->get();
and this is working. But now how can i get the
SELECT * FROM (..inner query..) client_id
WHERE (a.Country = 'AT' AND a.ZIP BETWEEN '0000' AND '5000')
convert to my query builder. sure i could use ->select() and write the raw sql query but i need this in the query builder because my inner and outer where clause i optional

I guess you could simplify your query as below, there is not need for sub query
SELECT a.Client, ad.Kto, a.Address, a.Matchcode, a.Name1, a.country, a.ZIP, a.Street
FROM [HeadQuarter].[dbo].[Addresses] a
INNER JOIN [HeadQuarter].[dbo].[AddressesDetails] ad
ON (a.Client = ad.Client AND a.Address = ad.Address)
WHERE ad.Active <> 0
AND a.Country = 'AT'
AND a.ZIP BETWEEN '0000' AND '5000'
AND (a.USER_emailactive = 0 OR a.USER_emailactive IS NULL)
In query builder you can use Parameter Grouping
\DB::connection('sqlsrv')
->table('[HeadQuarter].[dbo].[Addresses] as a')
->join('[HeadQuarter].[dbo].[AddressesDetails] as b', function($join){
$join->on('a.Client', '=', 'ad.Client')
->on('a.Address', '=', 'ad.Address');
})
->select('a.Client', 'ad.Kto', 'a.Address', 'a.Matchcode', 'a.Name1', 'a.country', 'a.ZIP', 'a.Street')
->where('ad.Active', '<>', '0')
->where(function ($query) {
$query->whereNull('a.USER_emailactive')
->orWhere('a.USER_emailactive', '=', '0');
})
->where(function ($query) {
$query->orWhere(function ($query) {
$query->where('a.Country', '<>', 'AT')
->whereBetween('a.ZIP', ['0000', '5000']);
})->orWhere(function ($query) {
$query->where('a.Country', '<>', 'Foo')
->whereBetween('a.ZIP', ['0000', '5000']);
})->orWhere(function ($query) {
$query->where('a.Country', '<>', 'Bar')
->whereBetween('a.ZIP', ['0000', '5000']);
});
})
->get();

Related

Eloquent query with COALESCE, IF, complex ON clause

i'd like to ask, how would you write the following query in Laravel Eloquent?
Please note the COALESCE, IF, and the complex LEFT JOIN ... ON (... AND ...).
SELECT COALESCE(IF(customer_group_id=6, prices.price, NULL), products.price) AS finalPrice, prices.customer_group_id, products.*, product_translations.*
FROM `product_categories`
LEFT JOIN `products` ON `product_categories`.`product_id` = `products`.`id`
LEFT JOIN `product_translations` ON `product_translations`.`product_id` = `products`.`id`
LEFT JOIN `prices`
ON (`products`.`id` = `prices`.`product_id` AND `prices`.`customer_group_id` = 6 )
WHERE `product_translations`.`locale` = 'it'
AND `products`.`online` = 1
AND `products`.`sellable` = 1
AND `category_id` = 22
UPDATE: By far, what i came up with is the following:
ProductCategory::leftjoin('products', 'product_categories.product_id', '=', 'products.id')
->leftJoin('product_translations', 'product_translations.product_id', 'products.id')
->leftJoin('prices', function($join) use($customer_group_id) {
$join->on('products.id', '=', 'prices.product_id')
->on('prices.customer_group_id', '=', DB::raw($customer_group_id));
})
->select(
DB::raw('coalesce(if(customer_group_id='.$customer_group_id.',prices.price,NULL), products.price) AS finalPrice'),
'prices.customer_group_id',
'products.*',
'product_translations.*'
)
->where('product_translations.locale', '=', $locale)
->where('products.online', '=', true)
->where('products.sellable', '=', true)
->where('category_id', '=', $this->id);

How to convert query mysql to laravel query

Hai all I have this raw query in mysql I need to translate to laravel 5.4 query builder:
SELECT MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah
FROM detail_brg_keluar AS dbk
INNER JOIN penjualan AS pen ON pen.id = dbk.id_brg_keluar
RIGHT JOIN calendar ON (DATE(pen.tgl_keluar) = calendar.datefield)
WHERE
(calendar.datefield BETWEEN
(SELECT MIN(DATE(tgl_keluar)) FROM penjualan AS p
INNER JOIN detail_brg_keluar AS detail ON p.id = detail.id_brg_keluar
WHERE detail.id_produk = 818002)
AND
(SELECT MAX(DATE(tgl_keluar)) FROM penjualan AS pe
INNER JOIN detail_brg_keluar AS det ON pe.id = det.id_brg_keluar
WHERE det.id_produk = 818002))
AND (dbk.id_produk = 818002 OR dbk.id_produk IS NULL)
GROUP BY MONTH(calendar.datefield)
ORDER BY calendar.datefield ASC
This my laravel code, and code error -_-
$proT = DB::table('detail_brg_keluar AS dbk')
->select( DB::raw('MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah '))
->join('penjualan AS pen', 'pen.id', '=', 'dbk.id_brg_keluar')
->join('calendar', DB::raw('DATE(pen.tgl_keluar)'), '=', 'calendar.datefield')
->whereBetween('calendar.datefield', [$from[0]->tanggalAwal, $to[0]->tanggalAkhir])
->where(DB::raw('dbk.id_produk = 818002 OR dbk.id_produk IS NULL'))
->groupBy(DB::raw('MONTH(calendar.datefield)'))
->orderBy(DB::raw('calendar.datefield', 'ASC'))
->get();
Code from and to :
$from = DB::table('penjualan AS p')
->select(DB::raw('MIN(DATE(tgl_keluar)) AS tanggalAwal'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id)
->get();
$to = DB::table('penjualan AS p')
->select(DB::raw('MAX(DATE(tgl_keluar)) AS tanggalAkhir'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id)
->get();
I have no idea anymore?
Thx all
Use this:
$from = DB::table('penjualan AS p')
->select(DB::raw('MIN(DATE(tgl_keluar)) AS tanggalAwal'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id);
$to = DB::table('penjualan AS p')
->select(DB::raw('MAX(DATE(tgl_keluar)) AS tanggalAkhir'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id);
$proT = DB::table('detail_brg_keluar AS dbk')
->select( DB::raw('MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah '))
->join('penjualan AS pen', 'pen.id', '=', 'dbk.id_brg_keluar')
->rightJoin('calendar', DB::raw('DATE(pen.tgl_keluar)'), '=', 'calendar.datefield')
->whereRaw('calendar.datefield BETWEEN ('.$from->toSql().') AND ('.$to->toSql().')', [$id, $id])
->where(function($query) {
$query->where('dbk.id_produk', 818002)->orWhereNull('dbk.id_produk');
})
->groupBy(DB::raw('MONTH(calendar.datefield)'))
->orderBy('calendar.datefield', 'ASC')
->get();
Don't execute $from and $to, use the generated SQL.
I replaced ->join('calendar' with ->rightJoin('calendar'.
I replaced where(DB::raw()) with a nested constraint.
orderBy() doesn't need a raw expression.

How to effectively using laravel 5.5 eloquent on complex join and group

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

Laravel Eloquent Complex Join Statement - specific example provided

I have the following raw SQL that I'm trying to convert into Eloquent form:
SELECT oe.eventID, oe.eventName, oet.etName, date_format(oe.eventStartDate, '%c/%d/%Y') as eventStartDate,
date_format(oe.eventEndDate, '%c/%d/%Y') AS eventEndDate
FROM `org-event` oe
JOIN `org-event_types` oet on oe.eventTypeID=oet.etID and oet.orgID=?
JOIN `event-registration` er on er.eventID = oe.eventID
WHERE (er.regStatus='Active' or er.regStatus='In Progress') AND personID=? AND oe.deleted_at is NULL
ORDER BY oe.eventStartDate DESC
I've crafted the following and I'm running into an error saying that '10' isn't a column. 10 is the value of $this->currentPerson->defaultOrgID
DB::table('org-event')
->join('org-event_types', function($join) {
$join->on('org-event_types.etID', '=', 'org-event.eventTypeID');
$join->on('org-event_types.orgID', '=', $this->currentPerson->defaultOrgID);
})->join('event-registration', 'event-registration.eventID', '=', 'org-event.eventID')
->where('event-registration.personID', '=', auth()->user()->id)
->where(function($w) {
$w->where('event-registration.regStatus', '=', 'Active')
->orWhere('event-registration.regStatus', '=', 'In Progress');
})
->select('org-event.eventID', 'eventName', 'etName', 'eventStartDate', 'eventEndDate')
->orderBy('org-event.eventStartDate')->get();
I dropped the deleted_at where clause because that's automatic using eloquent.
You have to join on the corresponding foreign key: oe.eventTypeID=oet.etID and then supply a where explaining what you want to filter your results by, i.e. ->where('orgID','=',$this->currentPerson->defaultOrgID).
DB::table('org-event')
->join('org-event_types', function($join) {
$join->on('org-event_types.etID', '=', 'org-event.eventTypeID');
$join->on('org-event_types.orgID', '=', 'org-event.eventTypeID')->where('orgID','=',$this->currentPerson->defaultOrgID);
})->join('event-registration', 'event-registration.eventID', '=', 'org-event.eventID')
->where('event-registration.personID', '=', auth()->user()->id)
->where(function($w) {
$w->where('event-registration.regStatus', '=', 'Active')
->orWhere('event-registration.regStatus', '=', 'In Progress');
})
->select('org-event.eventID', 'eventName', 'etName', 'eventStartDate', 'eventEndDate')
->orderBy('org-event.eventStartDate')->get();

Passing parameter bindings to Laravel query builder?

I currently have a SQL query which a fantastic stackoverflow member helped me with earlier today. At the moment it works absolutely fine and the way I want it to work.
The only problem is it's RAW SQL and I'd very much like to get this working with the Laravel Query Builder.
SORRY THIS IS AN EDIT - HERE IS THE ORIGINAL QUERY:
$addresses = DB::select(
DB::raw('
(SELECT
"Company" AS object_type_name,
companies.company_name AS object_name,
addresses.*
FROM
addresses
INNER JOIN
companies
ON
addresses.object_id = companies.id
WHERE
addresses.object_type = 2)
UNION ALL
(SELECT
"Job" AS object_type_name,
jobs.job_title AS object_name,
addresses.*
FROM
addresses
INNER JOIN
jobs
ON
addresses.object_id = jobs.id
WHERE
addresses.object_type = 4)
'));
Here is the code I have so far:
$bindings = array(
'soft_deleted' => 0,
'user' => 1,
'company' => 2,
'candidate' => 3,
'job' => 4,
);
$companies = DB::table('addresses')->select(
'addresses.*',
'companies.company_name as object_name'
)->where('addresses.soft_deleted', '=', 0)->join('companies', function($join) use ($bindings){
$join->on('addresses.object_id', '=', 'companies.id')
->where('addresses.object_type', '=', $bindings['company']);
});
$jobs = DB::table('addresses')->select(
'addresses.*',
'jobs.job_title as object_name'
)->join('jobs', function($join) use ($bindings){
$join->on('addresses.object_id', '=', 'jobs.id')
->where('addresses.object_type', '=', $bindings['job']);
});
$addresses = $companies->unionAll($jobs)->get();
With the code above I'm getting the following error:
SQLSTATE[HY093]: Invalid parameter number (SQL: (select addresses., companies.company_name as object_name from addresses inner join companies on addresses.object_id = companies.id and addresses.object_type = 2 where addresses.soft_deleted = 0) union all (select addresses., jobs.job_title as object_name from addresses inner join jobs on addresses.object_id = jobs.id and addresses.object_type = ?))
I've done hours worth of searching but I can't seem to find the answer to this little problem.
I managed to fix it. It was as simple as removing the ->where from within each join closure and chaining it to the join itself and not nesting it.
Before:
$companies = DB::table('addresses')->select(
'addresses.*',
'companies.company_name as object_name'
)->where('addresses.soft_deleted', '=', 0)->join('companies', function($join) use ($bindings){
$join->on('addresses.object_id', '=', 'companies.id')
->where('addresses.object_type', '=', $bindings['company']);
});
$jobs = DB::table('addresses')->select(
'addresses.*',
'jobs.job_title as object_name'
)->join('jobs', function($join) use ($bindings){
$join->on('addresses.object_id', '=', 'jobs.id')
->where('addresses.object_type', '=', $bindings['job']);
});
After:
$companies = DB::table('addresses')->select(
'addresses.*',
'companies.company_name as object_name'
)->where('addresses.soft_deleted', '=', 0)->join('companies', function($join){
$join->on('addresses.object_id', '=', 'companies.id');
})->where('addresses.object_type', '=', 2);
$jobs = DB::table('addresses')->select(
'addresses.*',
'jobs.job_title as object_name'
)->where('addresses.soft_deleted', '=', 0)->join('jobs', function($join){
$join->on('addresses.object_id', '=', 'jobs.id');
})->where('addresses.object_type', '=', 4);
$addresses = $companies->unionAll($jobs)->get();
Hope this helps someone.

Categories