How can I write the query like that on Laravel, btw I'm using DB
I followed this instruction
https://dba.stackexchange.com/questions/175786/join-multiple-tables-for-aggregates, but still not get it
mysql query:
SELECT inptkegiatan.IDKEGIATAN,
(SELECT COUNT(kuesioner.PERTANYAAN)
FROM kuesioner
WHERE inptkegiatan.IDKEGIATAN = kuesioner.IDKEGIATAN) as totalPertanyaan,
(SELECT SUM(hasilkuesioner.JAWABAN)
FROM hasilkuesioner
WHERE kuesioner.IDKEGIATAN = hasilkuesioner.IDKEGIATAN) as totalJawaban,
(SELECT COUNT(regiskegiatan.IDKEGIATAN)
FROM regiskegiatan
WHERE hasilkuesioner.IDKEGIATAN = regiskegiatan.IDKEGIATAN ) as totalUser
FROM inptkegiatan
LEFT JOIN kuesioner ON inptkegiatan.IDKEGIATAN = kuesioner.IDKEGIATAN
LEFT JOIN hasilkuesioner ON inptkegiatan.IDKEGIATAN = hasilkuesioner.IDKEGIATAN
LEFT JOIN regiskegiatan ON inptkegiatan.IDKEGIATAN = regiskegiatan.IDKEGIATAN
GROUP BY inptkegiatan.IDKEGIATAN
I tried this on my Laravel with that url, still not get it
$datatwo = DB::table('inptkegiatan')
->join('kuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'kuesioner.IDKEGIATAN')
->join('hasilkuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'hasilkuesioner.IDKEGIATAN')
->join('regiskegiatan', 'inptkegiatan.IDKEGIATAN', '=', 'regiskegiatan.IDKEGIATAN')
->where('IDNARASUMBER', '=', $value->PROFILEUSERS_ID)
->select('kuesioner.PERTANYAAN', 'hasilkuesioner.*', 'regiskegiatan.*',
DB::raw('count(DISTINCT(kuesioner.IDKEGIATAN)) + SUM(DISTINCT(hasilkuesioner.IDKEGIATAN)) as articles')
)
->groupBy('inptkegiatan.IDKEGIATAN')
->get();
First of all, use leftJoin instead of join, because join is alternate for innerJoin
$datatwo = DB::table('inptkegiatan')
->leftJoin('kuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'kuesioner.IDKEGIATAN')
->leftJoin('hasilkuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'hasilkuesioner.IDKEGIATAN')
->leftJoin('regiskegiatan', 'inptkegiatan.IDKEGIATAN', '=', 'regiskegiatan.IDKEGIATAN')
->where('inptkegiatan.IDNARASUMBER', '=', $value->PROFILEUSERS_ID)
->select([
'inptkegiatan.IDKEGIATAN',
\DB::raw('SELECT COUNT(kuesioner.PERTANYAAN)
FROM kuesioner
WHERE inptkegiatan.IDKEGIATAN = kuesioner.IDKEGIATAN) as totalPertanyaan'),
\DB::raw('(SELECT SUM(hasilkuesioner.JAWABAN)
FROM hasilkuesioner
WHERE kuesioner.IDKEGIATAN = hasilkuesioner.IDKEGIATAN) as totalJawaban'),
\DB::raw('(SELECT COUNT(regiskegiatan.IDKEGIATAN)
FROM regiskegiatan
WHERE hasilkuesioner.IDKEGIATAN = regiskegiatan.IDKEGIATAN ) as totalUser')
])
->groupBy('inptkegiatan.IDKEGIATAN')
->get();
Try with this. It should work
Related
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);
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.
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();
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
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.