我正在尝试查找具有相同id值的总和。
$tournament = Tournament::find($id);
$topTen = DB::table('match_scoreboard_batting')
->join('matches','match_scoreboard_batting.match_id','=','matches.id')
->where('matches.tournament_id',$id)
->select('match_scoreboard_batting.player_id','match_scoreboard_batting.runs')
->sum('match_scoreboard_batting.runs');
echo "<pre>";
print_r($topTen);
我了解所有ID运行总和。但是如何找到同一ID一起运行?
答案 0 :(得分:0)
请尝试此查询。
$topTen = DB::table('match_scoreboard_batting')
->join('matches','match_scoreboard_batting.match_id','=','matches.id')
->where('matches.tournament_id',$id)
->select('match_scoreboard_batting.player_id', \DB::raw(' sum(match_scoreboard_batting.runs) as runs'));
您可以使用:
来自laravel docs
DB::raw('SUM(price) as total_sales')
希望它对您有用。
答案 1 :(得分:0)
使用sum()
将导致自动按主键分组(我猜这里是player_id)。如果要获取每个比赛,锦标赛或球员的sum(),则应在查询中添加->groupBy('some_id')
。