Laravel Join返回重复数据

时间:2017-06-19 04:32:21

标签: php mysql laravel laravel-5 eloquent

我试图获得前五名玩家并且我使用join来获取基于personId的每个玩家信息,但我的查询返回重复数据。我无法弄清楚为什么它重复记录,任何帮助都会非常有用。感谢

DB::table('person_competition_statistics as pcs')
                    ->where('pcs.competitionId', $competitionId)
                    ->where('pcs.teamId', $teamId)
                    ->orderBy('pcs.sStealsAverage', 'desc')
                    ->rightJoin('players as player', 'pcs.personId', '=',  'player.personId')
                    ->select(['pcs.personId', 'pcs.sStealsAverage', 'player.firstName', 'player.familyName', 'player.playingPosition', 'player.image_thumb'])
                    ->take(5)
                    ->get();

结果如下

#items: array:5 [▼
      0 => {#444 ▼
        +"personId": 29872
        +"sStealsAverage": 1.24
        +"firstName": "Rhys"
        +"familyName": ""
        +"playingPosition": ""
        +"image_thumb": ""
      }
      1 => {#438 ▼
        +"personId": 29872
        +"sStealsAverage": 1.24
        +"firstName": "Rhys"
        +"familyName": ""
        +"playingPosition": ""
        +"image_thumb": ""
      }
      2 => {#439 ▼
        +"personId": 29872
        +"sStealsAverage": 1.24
        +"firstName": "Rhys"
        +"familyName": ""
        +"playingPosition": "GRD"
        +"image_thumb": ""
      }
      3 => {#441 ▼
        +"personId": 29872
        +"sStealsAverage": 1.24
        +"firstName": "Rhys"
        +"familyName": ""
        +"playingPosition": "GRD"
        +"image_thumb": ""
      }
      4 => {#435 ▼
        +"personId": 29872
        +"sStealsAverage": 1.24
        +"firstName": "Rhys"
        +"familyName": ""
        +"playingPosition": "GRD"
        +"image_thumb": ""
      }

3 个答案:

答案 0 :(得分:2)

我使用唯一来返回唯一的personId和 array_slice 来获得前5名,下面是我的代码。

var date = new Date();
// Value is "Mon Jun 19 2017 10:00:08 GMT+0000 (GMT)"    
date.setTimezoneOffset("+09.00"); // For example
// Value is "Mon Jun 19 2017 01:00:08 GMT+0000 (GMT)"

答案 1 :(得分:1)

我认为您应该将groupBy与您的唯一ID一起使用,而不要使用表名Like

 DB::table('person_competition_statistics as pcs')
                    ->where('pcs.competitionId', $competitionId)
                    ->where('pcs.teamId', $teamId)
                    ->orderBy('pcs.sStealsAverage', 'desc')
                    ->rightJoin('players as player', 'pcs.personId', '=',  'player.personId')
                    ->select(['pcs.personId', 'pcs.sStealsAverage', 'player.firstName', 'player.familyName', 'player.playingPosition', 'player.image_thumb'])
                    ->take(5)
                    ->groupBy('personId')
                    ->get();

答案 2 :(得分:0)

使用原始SQL尝试此操作:

    $sql = " SELECT t.personId, t.sStealsAverage, 
        player.firstName, player.familyName,
        player.playingPosition, player.image_thumb
  FROM
  (
      SELECT pcs.personId, pcs.sStealsAverage
      FROM person_competition_statistics AS pcs
      WHERE pcs.competitionId = $competitionId
      AND pcs.teamId = $teamId
      ORDER BY pcs.sStealsAverage DESC
      LIMIT 5
  ) t
  LEFT players AS player ON (t.personId = player.personId)";
  $result = DB::select($sql);