TransWikia.com

I want to get the total number of contents of several columns from a table

Stack Overflow Asked by user11974734 on December 5, 2021

Here below my code, but it error
here below the error
Integrity constraint violation: 1052 Column ‘id’ in group statement is ambiguous (SQL: select sum(d1 + d2 + d3 + d4 + d5) as total, responden., durasi. from responden inner join durasi on responden.id = durasi.responden_id group by id)

And here below my code

$durasi = DB::table('responden')
                ->join('durasi', 'responden.id', '=', 'durasi.responden_id')
                ->select(DB::raw('sum(d1 + d2 + d3 + d4 + d5) as total'), 'responden.*', 'durasi.*')
                ->groupBy('id')
                ->get();

        return view('hasil', compact('durasi'));

i hope someone help me to solve the problem.thanks

2 Answers

this happens when multiple tables share a column name, you have to mention the table name before the id in group by like below

$durasi = DB::table('responden')
            ->join('durasi', 'responden.id', '=', 'durasi.responden_id')
            ->select(DB::raw('sum(d1 + d2 + d3 + d4 + d5) as total'), 'responden.*', 'durasi.*')
            ->groupBy('responden.id')
            ->get();

    return view('hasil', compact('durasi'));

Answered by Monika on December 5, 2021

In your query when grouping by groupBy('id'), id needs table alias. It should be responden.id

Answered by Hirumina on December 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP