简体   繁体   中英

How do I update a table with fields selected from another table?

Although there are many questions similar to this, such as " Updating a record from another table ", but i could not get this working.

I have a query that selects and updates table sem_stdexamfinresmark . The select subquery returns multiple rows of data whose size may not be equal to the table being updated, but the update is now working.

The query looks like :

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';

EDIT:

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;

try something that looks more like:

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

这就是当一个人失去睡眠时发生的事情:(我只是在这里犯了一个愚蠢的错误,并添加了“和”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM