MySQL选择具有相同值的3行

In my "odds" table I have multiple rows with the same "match_id" I would like to choose only 3 rows per match and then move for another match and again choose 3 rows for that and so on.

Imagine it like distinct but with 3 rows, not only 1.

Is there any way to do it without a loop in laravel 5?

Here is my query which takes multiple (more than 3) rows with the same match_id.

    \DB::table('matches as m')
  ->select([    'o.id as odd_id',
                'o.match_id as match_id',
                'o.type_id as type_id',
                'o.outcome as outcome',
                'o.odd as odd',
                'm.date_hour as match_date'
            ])
  ->where('m.created_at','>=',\DB::raw('DATE_SUB(NOW(), INTERVAL 24 HOUR)'))
  ->where('m.status_desc_id','=','1')
  ->join('odds as o', function ($join) {
            $join->on('m.id', '=', 'o.match_id')
                 ->where('o.type_id', '=', 43);

        })
  ->groupBy('o.id')
  ->get();

Thanks.

You can do this by generating rank for each record grouping on match_id. Then you can retrieve only those records where rank<=3.

I have used one of my sample tables to write below sample query and it worked for me.(MYSQL)

Assume SurveyID is same as match_id in your case. Query:

Select SurveyId,comments,val,paramid,rank1
From
(
SELECT SurveyId as SurveyId
,a.comments as comments
,a.val as val
,a.paramid as paramid,
(
     CASE SurveyId
     WHEN @curType 
     THEN @curRow := @curRow + 1 
     ELSE @curRow := 1 AND @curType := SurveyId END
     ) AS rank1
FROM test.sample_table a,
(SELECT @curRow := 0, @curType := '') r
) tmp
WHERE rank1 <=3
ORDER BY SurveyId ASC

I think using the 'take(n)' constraint would work:

\DB::table('matches as m')
  ->select([    'o.id as odd_id',
                'o.match_id as match_id',
                'o.type_id as type_id',
                'o.outcome as outcome',
                'o.odd as odd',
                'm.date_hour as match_date'
            ])
  ->where('m.created_at','>=',\DB::raw('DATE_SUB(NOW(), INTERVAL 24 HOUR)'))
  ->where('m.status_desc_id','=','1')
  ->join('odds as o', function ($join) {
            $join->on('m.id', '=', 'o.match_id')
                 ->where('o.type_id', '=', 43);

        })
  ->groupBy('o.id')
  ->take(3)
  ->get();