12

我有两张桌子

  1. 它包含的学生表 (Student_id,school_code,name,year,...)
  2. 它包含的学校表(school_id、School_code、School_name、year 等.....)

我想根据学校代码和年份用学校代码表中的 school_id 列更新学生表中的 school_code 列。我有五年的数据。所以 school_id 每年都不同。

我的查询是

UPDATE Master.Student
   SET school_code=( select school_id from Master.school as sc
  JOIN master.student as st
    ON st.school_code=sc.school_code
 WHERE sc.year=x)
 WHERE st.year=x;

但它没有更新。我收到错误subquery returns more than one value

4

4 回答 4

28

当您可以直接使用子查询时,为什么要使用子查询?

UPDATE st
  SET st.school_code = sc.school_id 
FROM master.student AS st
  JOIN Master.school AS sc
ON st.school_code = sc.school_code
WHERE sc.year=x
  AND st.year=x;

有关详细信息,请参阅更新 (Transact-SQL)

于 2012-09-11T05:13:41.297 回答
3
UPDATE Master.Student
  SET school_code = sc.school_id 
FROM Master.school as sc
WHERE school_code = sc.school_code
  AND year = x
  AND st.year = x;
于 2012-09-11T05:10:14.557 回答
1

试试这个查询

UPDATE student SET school_code = c.school_id  
FROM student t
  INNER JOIN school c 
    ON t.school_code = c.school_code AND t.year = c.year
WHERE c.year=x
于 2012-09-11T05:26:11.410 回答
-1
Update Table B set column name (of table b) =x.column name (from Table A) from    
(    
Select column name from Table A a,Table B b    
where a.Column name=b.column name            
)x    
where Table b.Column name=x.Column name(of Table b)
于 2020-09-04T08:08:36.350 回答