I have the following table called 'RESULT' that I get values for, from another table called 'Temp_main'
The fields in the 'RESULT' are like the following:
StudentName | SujectName | Result
---------------------------------
Adam | Math | Fail
Bob | History | Pass
Catherine | Math | Pass
Dave | Science | Fail
Evan | History | Pass
Primary key here is (StudentName,SubjectName)
I use the following code to get the data into the table:
insert into result(studentName,subjectName,result)
select StudentName,SubjectName,result
from temp_main
The temp_main table does not have the same primary key, so there are duplicate key values when I insert the data into result table.
Assume that a student appears for a test on the same subject more than once. All his attempts are on the temp_main table, while only his best result is on the Result table.
I am trying to get a ON DUPLICATE KEY UPDATE
code that updates the result of a student-subject pair
if and only if the latest result is 'Pass'
I tried
insert into result(studentName,subjectName,result)
select StudentName,SubjectName,result
from temp_main
on duplicate key update result = case when temp_main(result) = 'Pass'
Then result(result)='Pass'
I am sure the this is crappy code. But I could not get any better solution.