Skip to content
Advertisement

how to turn Aggregates with multiple table into USE DB at laravel?

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement