2017-03-05 3 views
1

adm_no, subject_id, exam_idscore 필드의 결과 표가 있습니다. 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 점과 두 번째 점수를 얻었습니다. 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); 
      } 
관련 문제