使用 Laravel 获取学生在考试中的位置科目

Get Position subjects in exams by Students using Laravel

我有一个 table 名为 results 的结果,其中包含以下列 ID、Reg Number、Name、Subjects、Programmes、Year、Term、score。我想按学生获得每个科目的位置,其中 Programs = Art and year = 2017 and Term = 1.

        $getResults = DB::table('results')
        ->leftjoin(DB::raw('((SELECT regNumber, score, programmes,term,year, subject_position from 
        (SELECT regNumber, score, programmes,term,year, subject_position,
            CASE WHEN @prevRank = score THEN @curRank WHEN @prevRank := score THEN @curRank := @curRank + 1 END AS subject_position FROM results p, 
            (SELECT @curRank :=0, @prevRank := NULL) r2  

                          where programmes ="'.$getProgramme.'" and  `year` = "'.$getLevel.'" and term = "'.$getTerm.'"
                          and year = "'.$getYear.'" ORDER BY score Desc) as t3 )'), function($joinn)
        {
            $joinn->on('regNumber', '=', 'results.regNumber')                    

        })      

        ->where('programmes',$getProgramme)
        ->where('year',$getYear)
        ->where('term',$getTerm)
        ->where('level',$getLevel)
        ->get();

下面是我想要实现的。要获得阴影主题位置:

假设您使用 Eloquent,您将像这样表示您的实体:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Result extends Model
{
    protected $fillable = [
        'Reg Number',
        'Name',
        'Subjects',
        'Programmes',
        'Year',
        'Term',
        'score',
        'Subject Postion',
    ];
}

请注意,我使用的列名称与您发布的图片中的列名称相同。 由于它们与查询列不一致,我选择了图像,因为它包含有关您的 table 架构的更多信息。

所以对于您的查询:

i want get positions of every subject by student where Programmes = Art and year = 2017 and Term = 1.

你可以这样做

Result::where('Programmes', 'Art')
    ->where('year', 2017)
    ->where('Term', 1)
    ->get()
    ->pluck('Subject Postion');

再次请注意,我使用的列名称和值与您的示例相同。

我可能会标准化列名称,因为我觉得这可能会造成混淆,并且可能无助于您的思考。小写 snake_case 是我的选择。例如:

registration_number, name, subject, programme, year, term, score, subject_position

绝对要牢记前进!