我有一个名为“结果”,我得到的值,从另一个名为表如下表“Temp_main”使用条件更新 - 在重复键更新
在“结果”的字段是这样的:
这里StudentName | SujectName | Result
---------------------------------
Adam | Math | Fail
Bob | History | Pass
Catherine | Math | Pass
Dave | Science | Fail
Evan | History | Pass
主键是(StudentName,SubjectName)
我用下面的代码来获取数据到表:
insert into result(studentName,subjectName,result)
select StudentName,SubjectName,result
from temp_main
temp_main表不具有相同的主键,因此在将数据插入结果表时存在重复的键值。
假设一名学生不止一次出现在相同主题上的考试。他所有的尝试都在temp_main表上,而最好的结果是在结果表上。
我试图让ON DUPLICATE KEY UPDATE
代码当且仅当最新结果为“合格”
我试图
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'
更新一个学生的学科对 的结果,我相信这是糟糕的代码。但我无法得到更好的解决方案。
'最新成果'? temp_main列上有一个'resultDate'列,你没有告诉我们什么? – 2013-04-08 15:11:38
不,我的意思是temp_main列上的结果。对困惑感到抱歉。 – 2013-04-08 15:25:49
@KumaranSenapathy - 汤姆指的是SQL并不真正按照'顺序'来维护事情。确保特定“最新”(或其他)行的** ONLY **方法是在相关列中使用“ORDER BY”子句。如果你没有这样的专栏,那么基本上就是烤面包,因为所有的行都是相同的。 – 2013-04-08 15:49:48