简体   繁体   English

Where 子句中的未知列:Mysql 更新查询

[英]Unknown Column in Where Clause : Mysql Update Query

This is my update query:这是我的更新查询:

update grade 
  set grade='A'
where (select assignment*0.3+midExam*0.35+finalExam*0.35 as FINALSCORE 
       from taking) >= 85 
  and taking.student_id = grade.student_id 
  and taking.subject_id = grade.subject-id;

and the result is below结果如下

Unknown column 'taking.student_id' in 'where clause' 'where 子句'中的未知列'taking.student_id'

What am i doing wrong?我究竟做错了什么?

The table taking is inside a subquery then is not visible outside the subquery taking表在子查询内,然后在子查询外不可见

for your need you should use an update with join根据您的需要,您应该使用带有连接的更新

update grade 
INNER JOIN taking ON taking.student_id=grade.student_id
       and taking.subject_id=grade.subject_id 
         and taking.assignment*0.3+taking.midExam*0.35+taking.finalExam*0.35 >= 85
set grade='A'

for a general solution通用解决方案

you could try adding a case when for each grade您可以尝试为每个年级添加一个案例

update grade 
INNER JOIN taking ON taking.student_id=grade.student_id
       and taking.subject_id=grade.subject_id          
set grade = (case when taking.assignment*0.3+taking.midExam*0.35+taking.finalExam*0.35 >= 85 THEN  'A'
              when taking.assignment*0.3+taking.midExam*0.35+taking.finalExam*0.35 between 60.0 and 84.9 THEN 'B'
              ELSE 'C' END) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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