问题陈述:
如果讲师教授的课程数量少于1,则将薪水更新为30000,否则更新为35000 -
使用一个命令。
我编写的代码导致错误,请您告诉我为什么它不起作用以及我如何改进它。先谢谢了
更新
使用Oracle SQL
schema:
讲师 -> id、姓名、部门名称、工资
教 -> id、course_id、学期、年份
update i
set i.salary = case
when count(t.course_id) < 1 then 30000
else 35000
from (select * from instructor i inner join teaches t on i.id = t.id)
这是一个例子;我创建了自己的表格(因为您没有提供您的表格),我希望它有意义。
SQL> create table instructor (id_instructor number, salary number);
Table created.
SQL> insert into instructor values (1, 100);
1 row created.
SQL> insert into instructor values (2, 100);
1 row created.
SQL>
SQL> create table teaches (id_instructor number, id_course number);
Table created.
SQL> insert into teaches values (1, 1);
1 row created.
SQL> insert into teaches values (1, 2);
1 row created.
SQL>
由于 ID = 2 的教师“什么都不教”,因此他的工资应该是 30000。另一方面,ID = 2 的教师教 2 个课程,因此他将获得 35000。
SQL> update instructor i set
2 i.salary = (select case when count(*) < 1 then 30000
3 else 35000
4 end
5 from teaches t
6 where t.id_instructor = i.id_instructor);
2 rows updated.
SQL> select * from instructor;
ID_INSTRUCTOR SALARY
------------- ----------
1 35000
2 30000
SQL>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)