You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中如何按Level统计Top3分数总和实现团队总分?

Laravel实现按Level分组计算各Score Top3总和及团队总分

一、Controller 逻辑实现

假设你的对应模型为Score,数据表名为scores——你可以根据实际项目中的名称自行修改。下面用DB查询构造器实现,这种方式处理分组下的Top3求和更直接高效。

核心代码

use Illuminate\Support\Facades\DB;

public function getTeamScores()
{
    // 计算每个Level下Score1的Top3分数总和
    $score1Top3 = DB::table('scores')
        ->select('level', DB::raw('SUM(score1) as top3_score1'))
        ->whereIn('id', function ($query) {
            $query->select('id')
                ->from('scores as s2')
                ->whereColumn('s2.level', 'scores.level')
                ->orderBy('score1', 'desc')
                ->limit(3);
        })
        ->groupBy('level');

    // 同理计算Score2的Top3总和
    $score2Top3 = DB::table('scores')
        ->select('level', DB::raw('SUM(score2) as top3_score2'))
        ->whereIn('id', function ($query) {
            $query->select('id')
                ->from('scores as s2')
                ->whereColumn('s2.level', 'scores.level')
                ->orderBy('score2', 'desc')
                ->limit(3);
        })
        ->groupBy('level');

    // 计算Score3的Top3总和
    $score3Top3 = DB::table('scores')
        ->select('level', DB::raw('SUM(score3) as top3_score3'))
        ->whereIn('id', function ($query) {
            $query->select('id')
                ->from('scores as s2')
                ->whereColumn('s2.level', 'scores.level')
                ->orderBy('score3', 'desc')
                ->limit(3);
        })
        ->groupBy('level');

    // 合并三个结果,计算团队总分
    $teamScores = DB::table($score1Top3, 's1')
        ->joinSub($score2Top3, 's2', fn($join) => $join->on('s1.level', '=', 's2.level'))
        ->joinSub($score3Top3, 's3', fn($join) => $join->on('s1.level', '=', 's3.level'))
        ->select(
            's1.level',
            's1.top3_score1',
            's2.top3_score2',
            's3.top3_score3',
            DB::raw('s1.top3_score1 + s2.top3_score2 + s3.top3_score3 as team_total')
        )
        // 可选:按团队总分降序排序
        ->orderBy('team_total', 'desc')
        ->get();

    return view('team-scores', compact('teamScores'));
}

二、Blade 视图渲染

在resources/views/team-scores.blade.php中,直接遍历查询结果展示即可:

<div class="score-table">
    <table border="1" cellpadding="8" cellspacing="0">
        <thead>
            <tr>
                <th>Level</th>
                <th>Top3 Score1 总和</th>
                <th>Top3 Score2 总和</th>
                <th>Top3 Score3 总和</th>
                <th>团队总分</th>
            </tr>
        </thead>
        <tbody>
            @foreach($teamScores as $item)
                <tr>
                    <td>{{ $item->level }}</td>
                    <td>{{ $item->top3_score1 }}</td>
                    <td>{{ $item->top3_score2 }}</td>
                    <td>{{ $item->top3_score3 }}</td>
                    <td>{{ $item->team_total }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
</div>

关键说明

  • 如果某个Level下的记录不足3条,LIMIT 3会自动取现有所有记录求和,符合业务逻辑。
  • 若需要使用Eloquent模型实现,只需将DB::table('scores')替换为Score::query()即可,核心逻辑不变。
  • 可以根据项目UI需求调整Blade中的表格样式,比如添加CSS类美化。

内容的提问来源于stack exchange,提问作者Michael Ritung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 13:09:28