带总和的 Laravel 查询生成器

Muh*_*tti 2 mysql sum laravel-5

下面是查询,我遇到了这个错误

SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误

        $games = DB::table('games')
        ->select('start','dayofweek','name','ins_id','location_id',
            DB::raw('SUM(reservations) as reservations'),
            DB::raw('SUM(reservationsmax) as reservationsmax'),
            DB::raw('SUM(entries) as entries'),
            DB::raw('SUM(entriesmax) as entriesmax'),
            DB::raw('(SUM(entries) / SUM(reservationsmax) * 100 as percentage'))
        ->where('club_id', $club_id)
        ->whereBetween('start', [$from, $to])
        ->where('hide_from_customers', 0)
        ->where('external_id', 0)
        ->orderBy('name')
        ->groupBy('start')
        ->groupBy('dayofweek')
        ->groupBy('name')
        ->get();
Run Code Online (Sandbox Code Playgroud)

Muh*_*tti 5

我找到了答案,我认为这也会对其他人有所帮助。

$games = \App\Models\Games::select(DB::raw('start,dayofweek,name,instructor_id,location_id,SUM(reservations) as reservations,SUM(reservationsmax) as reservationsmax,SUM(entries) as entries,SUM(entriesmax) as entriesmax,(SUM(entries) / SUM(reservationsmax)) * 100 as percentage'))
            ->where('club_id', $club_id)
            ->whereBetween('start', [$from, $to])
            ->where('hide_from_customers', 0)
            ->where('external_id', 0)
            ->orderBy('name')
            ->groupBy('start')
            ->groupBy('dayofweek')
            ->groupBy('name')
            ->get();
Run Code Online (Sandbox Code Playgroud)