UpdateError:接收错误 ORA - 01427 单行子查询返回多于一行

use*_*149 5 sql oracle ora-01427 sql-update

我正在尝试根据同一个表(学生表)中的另一列和另一个表(学校表)中的列来更新列

代码是:

update student_table
set student_code =
(select l.student_code
from school_table l, student_table n
where l.school = n.schoolname)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

ORA-01427 单行子查询返回多于一行

任何帮助,将不胜感激。

Joh*_*yle 5

如果运行子查询,您会发现它返回不止一行。您正在尝试将一列更新为等于子查询的结果,因此它只需要一个值。您应该限制您的子查询仅返回一行,例如使用 max() 或 min(),或者,也许您打算连接到外部 Student_table?尝试:

update student_table n
set student_code =
(select l.student_code
from school_table l
where l.school = n.schoolname);
Run Code Online (Sandbox Code Playgroud)


小智 4

对您想要完成的任务有一个简单的英语解释会很有帮助。话虽如此,在我看来,您可以使用以下 SQL [假设 school_table 和 Student_table 之间存在一对多关系] 来完成您想要做的事情,将内部 select 作为与外部 update 语句相关的子查询:

update student_table 
set student_code = (select l.student_code 
                    from school_table 
                    where school_table.school = student_table.schoolname) 
;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。

问候,罗杰