是否可以向update语句添加select和condition子句

时间:2017-11-05 15:43:18

标签: mysql

我有一个基于年和学期的学生考试记录数据库,我希望在特定年份和学期中更改所有学生的水平,并使用以下代码给我一个错误。

update result
set level = ( select new_level.student 
               from new_level
               where new_level.student = result.student
              limit 1
              )
and year = '2016/2017'
and semester = 2

将会对解决此问题提供一些帮助。谢谢

2 个答案:

答案 0 :(得分:1)

您错过了UPDATE语句中的WHERE。

update result
set level = ( select new_level.student 
               from new_level
               where new_level.student = result.student
              limit 1
              )
where #what you missed.
    year = '2016/2017'
  and
    semester = 2

答案 1 :(得分:0)

您需要一个where子句和一个带有join的更新,以便在所涉及的表中加入学生

update result
inner join  new_level on new_level.student = result.student
set result.level =  new_level.student 
where year = '2016/2017'
and semester = 2

或者你可以直接将条件添加到ON子句(避免where where),例如:

update result
inner join  new_level on new_level.student = result.student 
       and year = '2016/2017'
         and semester = 2
set result.level =  new_level.student