查询基于id的多列的sum方法

时间:2017-08-23 04:35:26

标签: php laravel

我正在开发一个体育团队应用程序并试图总计(总计)整个赛季每个球员的得分。我有一个查询如下:

    $GMC = DB::table('matchcards')->where('grade_id', $gradeId)->select('id')->get();
        foreach($GMC as $object)
        {
            $arrays[] =  (array) $object;
        }
        $GMCX = collect($arrays)->flatten();

    foreach ($GMCX as $mc) { //Loop 1 - Get a list of all matchcards for this grade

        $im = DB::table('matchcards')->distinct()->select('capt', 'gk', 'player3', 'player4', 'player5', 'player6', 'player7', 'player8'
            , 'player9', 'player10', 'player11', 'player12', 'player13', 'player14', 'player15', 'player16')->where('id', $mc)->get();

        foreach ($im as $object2) {
            $arrays2[] = (array)$object2;
        }

        $imx = collect($arrays2)->flatten(); //Gets all the PlayedIDs from above loop
    } //end of Loop 1

        $gp ='0'; $gamesplayed= array(); $collection = collect(); $M='0';
        foreach ($imx as $p) { //loop 2 - take each id and count games played
            $_GET['p'] = $p;
            $pcount = DB::table('matchcards')->where('capt', $p)->where('grade_id', $gradeId)->orWhere('gk', $p)->orWhere('player3', $p)
                ->orWhere('player4', $p)->orWhere('player5', $p)->orWhere('player6', $p)->orWhere('player7', $p)
                ->orWhere('player8', $p)->orWhere('player9', $p)->orWhere('player10', $p)->orWhere('player11', $p)
                ->orWhere('player12', $p)->orWhere('player13', $p)->orWhere('player14', $p)->orWhere('player15', $p)
                ->orWhere('player16', $p)->get();
            $gp = count($pcount);
            if (count($pcount) > 0){ //Get Players Full Name
                $pn = DB::table('players')->select('id', 'fname', 'lname')->where('id', $p)->get();
                if (count($pn)>0) {
                    $pname = $pn[0]->fname." ".$pn[0]->lname;
                }

   $M = DB::table('matchcards')
                    ->select('p1scorer', 'p1goals','p2scorer', 'p2goals', 'p3scorer', 'p3goals','p4scorer', 'p4goals', 'p5scorer', 'p5goals','p6scorer', 'p6goals')
                    ->where('grade_id', '=', $gradeId)
                    ->Where(function ($query) {
                        $p = $_GET['p'];
                        $query->where('p1scorer', '=', $p)
                            ->orWhere('p2scorer', '=', $p)
                            ->orWhere('p3scorer', '=', $p)
                            ->orWhere('p4scorer', '=', $p)
                            ->orWhere('p5scorer', '=', $p)
                            ->orWhere('p6scorer', '=', $p);
                    })
                   ->sum(DB::raw('p1goals + p2goals + p3goals + p4goals + p5goals + p6goals'));
                   //->select(DB::raw('sum(p1goals + p2goals + p3goals + p4goals + p5goals + p6goals ) as goals_scored'))->get();

}

           //Add all objects into collection/
            $collection->push(['id' => $p, 'gp' => $gp, 'pname' => $pname, 'gs' => $M]);

        } //end of loop 2

        $unique = $collection->unique();  //Removed duplicate IDs

只有得分的人才会回归 - 所以它的一半工作。我只是不知道如何将该用户的目标加起来(跨越6个可能的列)。目前,我的查询将该特定比赛/游戏(或DB中的行)的所有目标添加到玩家。

每个匹配卡有6个可能的列,教练可以选择最多6个得分的玩家(因此,p1scorer和p1goals属于一起,p2scorer和p2goals属于一起......每周玩家ID可以在任何一个P1scorer中到p6scorer列 - 但从不在同一行重复)。场景示例第1周(第1行)p1scorer是17(玩家的ID),得分为2个目标(所以2个在p1goals内)。另外p2scorer是5谁得分1个目标 - 总共3个目标 - 想要发送到该行的数组(id:17个目标:2} {id:5个目标:1},并循环所有其他行和ADD为整个赛季中得分的每个人提供现有统计数据的目标。

希望有人能够在这里帮助我...提前感谢...

1 个答案:

答案 0 :(得分:0)

你应该试试这个:

使用Illuminate \ Http \ Request; 使用输入;

$p = $request->p;

$p = Input::get(p);   


 $M = DB::table('matchcards')
                    ->select('p1scorer', 'p1goals','p2scorer', 'p2goals', 'p3scorer', 'p3goals','p4scorer', 'p4goals', 'p5scorer', 'p5goals','p6scorer', 'p6goals')
                    ->where('grade_id', '=', $gradeId)
                    ->Where(function ($query) use($p) {

                        $query->where('p1scorer', '=', $p)
                            ->orWhere('p2scorer', '=', $p)
                            ->orWhere('p3scorer', '=', $p)
                            ->orWhere('p4scorer', '=', $p)
                            ->orWhere('p5scorer', '=', $p)
                            ->orWhere('p6scorer', '=', $p);
                    });

                   ->sum(DB::raw('p1goals + p2goals + p3goals + p4goals + p5goals + p6goals'));