如何在Laravel

时间:2017-03-21 04:16:06

标签: mysql laravel

我正在开展一个项目,我需要帮助。我需要检查学生的成绩,如果他选择了2次主题,然后取得该等级的更大值。让我们说他第一次尝试E,然后他再次接受并得到B.或者在其他情况下,在他的第一次尝试中他得到C,然后当他重新接受该主题时,他得到了D。

这是我的表

id_krs | id_mhs | id_sub | status | grade 
------ | ------ | ------ | ------ | ------
1      | 3      | 1      | B      | A
2      | 3      | 3      | B      | A
3      | 3      | 4      | B      | E
4      | 3      | 4      | U      | B
5      | 3      | 7      | B      | C
6      | 3      | 7      | U      | E

B = 1,U =重试。

修改

我很抱歉这个问题不明确。 我的问题是

 $user=Auth::user()->username;
    $mhs = DB::table('krs as f')
        ->select('f.id_sub','f.status','f.grade','f.id_krs',DB::raw("(
                        CASE 
                            when f.grade= 'E' then 0 
                            when f.grade= 'D' then 1 
                            when f.grade= 'D+' then 1.5 
                            when f.grade= 'C' then 2 
                            when f.grade= 'B' then 3
                            when f.grade= 'B+' then 3.5
                            when f.grade= 'A' then 4 end 
                        )   
                        AS angka_kredit"))
                        ->join('mhs_pt as a','a.id_mhs_pt','=','f.id_mhs_pt')
                        ->orderBy('angka_kredit','asc')
                        ->groupBy('f.id_sub')                           
                        ->where('a.no_mhs',$user)
                        ->get();

当我将结果变成json时,结果是

[{"id_sub":30,"status":"B","grade":"D","id_krs":45,"angka_kredit":"1.0"},
{"id_sub":31,"status":"B","grade":"C","id_krs":46,"angka_kredit":"2.0"},
{"id_sub":29,"status":"B","grade":"A","id_krs":38,"angka_kredit":"4.0"}]

而我表中的数据是

+--------+-----------+----------+--------+-------+
| id_krs | id_mhs_pt | id_sub   | status | grade | 
+--------+-----------+----------+--------+-------+
|     38 | 3         | 29       | B      | A     | 
|     45 | 3         | 30       | B      | D     |  
|     46 | 3         | 31       | B      | C     | 
|  18393 | 3         | 31       | U      | A     | 
+--------+-----------+----------+--------+-------+

我的意思是结果查询应该显示id_krs 18393而不是46,因为它具有更高的等级。非常感谢!

1 个答案:

答案 0 :(得分:0)

假设您的模型名称为:StudentGrade,当前主题ID为4,当前学生ID为3,您可以尝试此查询:

$grade = StudentGrade::where('id_sub', '=', 4)->where('id_mhs', '=', 3)->orderBy('grade', 'asc')->first();

希望这可以帮助你找到正确的方向:)