如何使用从另一个表中选择的字段更新表?

时间:2011-06-20 04:18:42

标签: mysql ruby database sequel

虽然有很多类似的问题,例如 “Updating a record from another table”,但我无法解决这个问题。

我有一个选择和更新表sem_stdexamfinresmark的查询。 select子查询返回多行数据,其大小可能不等于正在更新的表,但更新现在正在运行。

查询如下:

update  sem_stdexamfinresmark sr,
    (select 
         se.currsession,
         str.studentid,
         str.classid,
         str.subjectid,
         str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
         str.aggGrade
    from 
        sem_stdexamtermresr str,
        sem_exam se 
    where 
        str.examid=se.examid and 
        se.examtype = 'Second Term' and
        se.currsession =1 and classid='8' 
     ) s
     set 
        sr.SecondTermMark = s.aggPer and
        sr.SecondTermGrade = s.aggGrade 
     where
        sr.studentid=s.studentid and 
        sr.subjectid=s.subjectid and 
        s.currsession = s.currsession and
        sr.classid='8';

编辑:

update  sem_stdexamfinresmark 
 set 
    sr.SecondTermMark = s.aggPer and
    sr.SecondTermGrade = s.aggGrade 
from 
(select 
     se.currsession,
     str.studentid,
     str.classid,
     str.subjectid,
     str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
     str.aggGrade
from 
    sem_stdexamtermresr str,
    sem_exam se 
where 
    str.examid=se.examid and 
    se.examtype = 'Second Term' and
    se.currsession = 1 and classid='8' 
 ) s
 where
    sr.studentid=s.studentid and 
    sr.subjectid=s.subjectid and 
    s.currsession =1 and
    sr.classid='8';
    select * from sem_exam;
    update sem_exam set currsession =1;

2 个答案:

答案 0 :(得分:0)

尝试看起来更像的东西:

update foo
set col = bar.col
from bar
where ...

答案 1 :(得分:0)

当一个人失眠时会发生这种情况:(我只是在这里做了一个愚蠢的错误并添加了“和”

相关问题