下面是查询,我遇到了这个错误
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();
最佳答案
我找到了答案,我认为这对其他人也有帮助。
$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();
关于mysql - 带总和的 Laravel 查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43933246/