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

标签 mysql ruby database sequel

虽然类似这样的问题还有很多,比如 “Updating a record from another table ”,但我无法让它工作。

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

查询看起来像:

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;

最佳答案

尝试一些看起来更像的东西:

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

关于mysql - 如何使用从另一个表中选择的字段更新表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6406776/

相关文章:

python - 使用python获取MySQL数据库列表

php - MySQL(和 PHP)中实体的修订跟踪框架?

java - 批量更新从更新中返回了意外的行数(Spring/Hibernate)

Ruby:如何编写像 map 这样的 bang 方法?

javascript - 将js文件导入到webpack应用程序js中

ruby-on-rails - 注册失败后设计重定向

database - Oracle 什么时候会停止使用 9i 数据库?

mysql - NOT EXISTS 条件下的查询速度问题

mysql - 我在 mysql 查询中遇到错误?

php - 检查存储的日期增量之间的日期增量是否存在