2017-03-05 7 views
1

adm_no,subject_id,exam_idおよびscoreフィールドのテーブルがあります。私はlaravel 5.4のupdateOrCreateメソッドを使ってデータを挿入しました。重複を排除しながらクエリビルダーを使用してデータを出力する方法

私がデータを取得するとき、私はまだ重複を取得します。例えばユーザーが特定の科目の例2の学生の印を挿入し、同じ学生と科目についてそれを繰り返すと、すべての結果の最新行を取得する必要があります。ここで

は、私のクエリのサンプルです:

public function searchStudent() 
{ 
    $exam = Exam::all(); 
    $term = Term::all(); 

    $a = Input::get('adm_no'); 
    $e = Input::get('exam_id'); 
    $t = Input::get('term_id'); 
    $y = Input::get('year'); 

    $results = DB::table('results') 
     ->select(DB::raw('DISTINCT(subject_id)')) 
     ->where('adm_no', 'LIKE', '%' . $a . '%') 
     ->where('exam_id','LIKE', '%' . $e . '%') 
     ->where('term_id', 'LIKE', '%' . $t . '%') 
     ->where('year', 'LIKE', '%' . $y . '%') 
     ->orderBy('created_at', 'desc') 
     ->get(); 

    dd($results); 
} 

答えて

0

は、次のクエリ試してみてください。

$results = DB::table('results') 
     ->groupBy('subject_id') 
     ->where('adm_no', 'LIKE', '%' . $a . '%') 
     ->where('exam_id','LIKE', '%' . $e . '%') 
     ->where('term_id', 'LIKE', '%' . $t . '%') 
     ->where('year', 'LIKE', '%' . $y . '%') 
     ->orderBy('created_at', 'desc') 
     ->get(); 
+0

そのそれでも最新の値を選ぶません。 adm_no:1、subject_id:1の最初のレコードを言い、同じ生徒の35点と2点目を40点、40点を選んで35点を無視する方法を教えてください。 –

+0

ああ、これはちょっと質問を変え、チェックアウトしようとする[この質問](http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group) – Desh901

0

を私は質問の詳細な研究を行なったし、これは私が私のために働いて得たものです。

質問

 public function searchStudent(){ 
     $a= Input::get('adm_no'); 
     $e= Input::get('exam_id'); 
     $t= Input::get('term_id'); 
     $y= Input::get('year'); 
     $results=DB::table('results')->distinct('subject_id') 
     ->where('adm_no', $a) 
     ->where('exam_id', $e) 
     ->where('term_id',$t) 
     ->where('year',$y) 
     ->groupBy('subject_id') 
     ->latest('subject_id') 
     ->get(); 
     dd($results); 
      } 
関連する問題