【问题标题】:Replacing a table value with the previous expression in oracle用oracle中的前一个表达式替换表值
【发布时间】:2014-04-11 07:13:04
【问题描述】:

好的,所以我有以下情况

我有一个名为 employees 的表,并且在以下情况下必须替换其中一些人的姓氏: 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')

得到一个错误,指出缺少右括号,我知道这并不能说明问题所在。我知道我遗漏了一些东西,部分 sintaxis 是错误的,我可能需要创建另一个表并添加这些值,以便它们保存在那里,我可以将它们与原始值进行比较,但在这一点上我完全被阻止并且无法发现我在做什么错误。请帮帮我,我真的很感激!

【问题讨论】:

    标签: sql oracle join subquery dml


    【解决方案1】:

    您对语句中的更新内容和更新内容感到困惑。

    这是要更新的内容。我使用 IN 子句来说明这一点。 EXISTS 子句也是合适的。

    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
    )
    

    【讨论】:

    • 索斯滕·凯特纳!!!非常感谢,这正是我要找的!!!是的,我完全糊涂了,我为此做了很多周转,以至于我没有看到我只需要保持简单! :D欣赏它:D
    • 我只是出现一个错误,提示无法将 (table.name.....) 更新为 null,内部似乎有一个值为 null,我该如何更改它以使其工作?
    • 我看到发生此错误的唯一原因是 first 记录需要更新,当然之前没有记录。所以不能满足要求,语句失败是正确的。为了避免这种情况,您可以:1)使 last_name 可以为空,或 2)更改规则(例如,当之前没有记录时保留名称)并相应地更改语句。
    • 好的太好了!!我实际上可以通过更改 last_name nullable 来解决它 :) 再次感谢!
    【解决方案2】:

    您应该使用分析lag 函数,然后如果例如员工 172 不存在并且您必须将员工 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');
    

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 2016-03-17
      相关资源
      最近更新 更多