好的,所以我有以下方案

我有一个名为员工的表,并且必须在以下条件下替换其中一些人的姓氏: 1-姓氏必须仅替换到牛津工作的员工。 2-他们的新姓氏将是员工编号-1的人的姓氏(例如员工#173现在应该拥有员工#172姓名而不是)

这就是我开始查询的方式:

select last_name,num_emp

from employees

where num_emp in(

select e.num_emp

from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')
.

并且我做了第二个查询,以确保将哪个值替换哪个值替换哪个

Select last_name,num_emp

from employees

where num_emp in(

select (e.num_emp-1)

from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')
.

现在我以为我可以做到这一点并制作代码工作......但它没有:

update employees

set last_name=(select last_name

from employees

where num_emp in(

select (e.num_emp-1)
from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')
.

错误地说sql命令的意外结束...

所以我想改变,因为我相信集合上有太多的价值不是点,这里是我上次做的方式:

update employees

set last_name=(select last_name
from employees)

where  num_emp =(

select (e.num_emp-1)
from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')
.

出现了一个错误,说缺少左括号,我知道它没有表达Whaat问题是。我知道我错过了一些东西,部分sintaxis是错误的,而且我可能需要喋喋不休地叮叮当当地添加那些值,以便他们得到那里的那些值,我可以将它们与原始的桌子进行比较,但我完全可以比较它们被封锁,无法发现我正在做的错误是什么。请帮帮我,我真的很了解它!

有帮助吗?

解决方案

您正在与在您的语句中更新和更新的内容混淆。

这是该更新的内容。我用条款来制作平原。存在条款也是合适的。

update employees
set last_name = ...
where num_dept in
(
  select num_dept
  from departments
  where id_office in
  (
    select id_office
    from office
    where city = 'Oxford'
  )
);
.

,这里是用:

更新的内容
set last_name =
(
  select last_name
  from employees prev_employee
  where prev_employee.num_emp = employee.num_emp - 1
)
.

其他提示

您应该使用分析生成的icetagcode函数,如果例如,如果员工172不存在,则填写间隙,并且您必须在173中将员工171的名称放在173中。

您的选择应该是这样的东西

with new_emp as
   (select last_name,lag(last_name, 1, 0) over (order by num_emp) as new_last_name, num_emp
    from employees)
select *
from new_emp
where num_emp in(
   select e.num_emp
   from employees e
   join departments using(num_dept)
   join office using(id_office)
   where city='Oxford');
.

此选择将为您提供原始姓氏,新的姓氏,员工编号。

然后之后您的更新应该如下:

update employees x
set last_name = (with new_emp as
                  (select last_name,lag(last_name, 1, 0) over (order by num_emp) as new_last_name, num_emp
                   from employees)
                select new_last_name
                from new_emp ne
                where ne.num_emp = x.num_emp)
where x.num_emp in(
   select e.num_emp
   from employees e
   join departments using(num_dept)
   join office using(id_office)
   where city='Oxford');
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top