mysql - 带总和的 Laravel 查询生成器

标签 mysql laravel-5 sum

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

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/

相关文章:

php - 再次编辑一行时出现问题,它在 php codeigniter 中插入新行

php - PHPWord中模板处理器的默认目录是什么?

php - 范围方法的 where 子句中的 Laravel 数据透视表计数

laravel - 路由参数的构造函数注入(inject)

mysql - 基于多个条件和 JOINS 进行一对多求和

javascript - 使用 .reduce 为每个名字求和点

php - 使用 Jquery 进行表排序

mysql - 另一个 MySQL MAX() 问题

c# - 在新列中按总和显示分组

MySQL 统计记录并使用权重