Laravel如何获得两个结果值的平均值

时间:2017-07-04 01:01:56

标签: php mysql sql laravel laravel-5

所以我有一个查询可以返回上一季和当前季节的所有玩家统计数据。我想要做的是从上赛季和当前赛季中获得一名球员的平均值,并且只进入一个对象。以下是我的查询和样本结果。

查询

public static function getAllTimeLeaders($request) {
        $competitionIds = self::getLasTwoSeason();
        $data = DB::table('leader_board_stats as lbs')
            ->select([
                'sGamesStarted',
                'sMinutesAverage',
                'sPointsAverage',
                'sFieldGoalsMadeAverage',
                'sFieldGoalsAttemptedAverage',
                'sFieldGoalsPercentage',
                'sTwoPointersMadeAverage',
                'sTwoPointersAttemptedAverage',
                'sThreePointersMadeAverage',
                'sThreePointersAttemptedAverage',
                'sThreePointersPercentage',
                'sFreeThrowsMadeAverage',
                'sFreeThrowsAttemptedAverage',
                'sReboundsTotalAverage',
                'sAssistsAverage',
                'sStealsAverage',
                'sBlocksAverage',
                'sTurnoversAverage',
                'sFoulsPersonalAverage',
                'firstName',
                'familyName',
                'personId',
                'teamId',
                'competitionId'
            ])
            ->where('lbs.periodNumber', '0')
            ->whereIn('lbs.competitionId', [$competitionIds[0]->competitionId, $competitionIds[1]->competitionId])
            ->get();
        echo '<pre>';
        var_dump($data);
    }

返回值的示例

[
    [0]=> object(stdClass)#262 (24) {
      ["sGamesStarted"]=>
      int(6)
      ["sMinutesAverage"]=>
      float(17.66)
      ["sPointsAverage"]=>
      float(7.71)
      ["sFieldGoalsMadeAverage"]=>
      float(2.59)
      ["sFieldGoalsAttemptedAverage"]=>
      float(5.62)
      ["sFieldGoalsPercentage"]=>
      float(0.46)
      ["sTwoPointersMadeAverage"]=>
      float(1.47)
      ["sTwoPointersAttemptedAverage"]=>
      float(2.53)
      ["sThreePointersMadeAverage"]=>
      float(1.12)
      ["sThreePointersAttemptedAverage"]=>
      float(3.09)
      ["sThreePointersPercentage"]=>
      float(0.36)
      ["sFreeThrowsMadeAverage"]=>
      float(1.41)
      ["sFreeThrowsAttemptedAverage"]=>
      float(1.76)
      ["sReboundsTotalAverage"]=>
      float(2.85)
      ["sAssistsAverage"]=>
      float(1.71)
      ["sStealsAverage"]=>
      float(0.68)
      ["sBlocksAverage"]=>
      float(0.21)
      ["sTurnoversAverage"]=>
      float(0.91)
      ["sFoulsPersonalAverage"]=>
      float(2.35)
      ["firstName"]=>
      string(3) "Person1"
      ["familyName"]=>
      string(8) "Familyname"
      ["personId"]=>
      int(28831)
      ["teamId"]=>
      int(3749)
      ["competitionId"]=>
      int(9224)
    },
    [88]=> object(stdClass)#527 (24) {
      ["sGamesStarted"]=>
      int(11)
      ["sMinutesAverage"]=>
      float(23.28)
      ["sPointsAverage"]=>
      float(9.5)
      ["sFieldGoalsMadeAverage"]=>
      float(3.07)
      ["sFieldGoalsAttemptedAverage"]=>
      float(7.97)
      ["sFieldGoalsPercentage"]=>
      float(0.38)
      ["sTwoPointersMadeAverage"]=>
      float(1.4)
      ["sTwoPointersAttemptedAverage"]=>
      float(3.47)
      ["sThreePointersMadeAverage"]=>
      float(1.67)
      ["sThreePointersAttemptedAverage"]=>
      float(4.5)
      ["sThreePointersPercentage"]=>
      float(0.37)
      ["sFreeThrowsMadeAverage"]=>
      float(1.7)
      ["sFreeThrowsAttemptedAverage"]=>
      float(2.2)
      ["sReboundsTotalAverage"]=>
      float(3.57)
      ["sAssistsAverage"]=>
      float(1.43)
      ["sStealsAverage"]=>
      float(0.93)
      ["sBlocksAverage"]=>
      float(0.23)
      ["sTurnoversAverage"]=>
      float(0.97)
      ["sFoulsPersonalAverage"]=>
      float(2.77)
      ["firstName"]=>
      string(3) "Person1"
      ["familyName"]=>
      string(8) "Familyname"
      ["personId"]=>
      int(28831)
      ["teamId"]=>
      int(3749)
      ["competitionId"]=>
      int(2254)
    }
]

2 个答案:

答案 0 :(得分:1)

尝试:avg()更简单,更清洁。

文档:https://laravel.com/docs/5.4/collections#method-avg

答案 1 :(得分:0)

最后我想通了我只需要使用每个统计字段的DB :: raw和AVG,然后按personId对其进行分组。

{{1}}