Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在Laravel中获得相同id值的总和?_Mysql_Laravel - Fatal编程技术网

Mysql 如何在Laravel中获得相同id值的总和?

Mysql 如何在Laravel中获得相同id值的总和?,mysql,laravel,Mysql,Laravel,我试图找到相同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)

我试图找到相同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);
$tournament=tournament::find($id);
$topTen=DB::表格('match\u scoreboard\u batting')
->加入('matches'、'match\u scoreboard\u batting.match\u id'、'='、'matches.id')
->其中('matches.touring_id',$id)
->选择('match\u scoredboard\u batting.player\u id','match\u scoredboard\u batting.runs')
->总和(“比赛、记分牌、击球、跑动”);
回声“;
印刷(前十名);
我知道如何得到所有id值的总和。但是如何获得相同id值的总和?

请尝试此查询

DB::raw('SUM(price) as total_sales')
您可以使用:

来自拉威尔


希望它能对您起作用。

使用
sum()
将导致自动按主键分组(我猜这里是玩家id)。如果您想获得每场比赛、锦标赛或球员的sum(),您应该在查询中添加
->groupBy('some_id')

您可以使用group by并将聚合函数sum放入要添加值的列中。谢谢兄弟。。它起作用了
DB::raw('SUM(price) as total_sales')