Search code examples
mysqlrubydatabasesequel

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;

Solution

  • This is what happens when one loses sleep :( I just did a silly mistake here and added "and"