【问题标题】:How to update One table column values with another table's column values? [duplicate]如何用另一个表的列值更新一个表的列值? [复制]
【发布时间】:2010-12-07 16:14:01
【问题描述】:

我有一个名为 Student 的表,其中包含 uniquename、age、department、city、Homecountry 列和另一个名为 Employee 的表,其中包含 uniquename、exp、qualification、Homecountry 列。

现在我想在 where 条件 Student.uniquename = Employee.uniquename 和 Student.Homecountry = Employee.Homecountry 下使用 Employee 表的资格列值更新 Student 表的部门列。

请帮我写更新语句。

【问题讨论】:

    标签: oracle


    【解决方案1】:

    这种查询称为相关子查询。根据您的要求,查询如下......

    update students s
      set s.department = (
              select e.qualification
                from employee e
                where s.uniquename = e.uniquename 
                  and s.Homecountry = e.Homecountry
           );
    

    根据您在下面的回复更新此帖子。

    再次重申,始终发布 create table 和 insert 语句(以及预期结果)以重现您的案例。如果您没有看到预期的结果,或者您在执行查询时看到错误,请发布确切的消息,而不是仅仅说“不工作”。这是我的 sqlplus 会话的结果。

    ---创建表和插入语句

    create table student(
         name varchar2(20),
         age  number,
         department varchar2(3),
         HomeCountry varchar2(10)
        );
    
    Table created.
    
    create table employee5(
         name varchar2(20),
         exp  number,
         qualification varchar2(3),
         homecountry varchar2(10)
       );
    
    Table created.
    
    insert into student values ('Mohan',25,'EEE','India');
    insert into student values ('Raja',27,'EEE','India');
    insert into student values ('Ahamed',26,'ECE','UK');
    insert into student values ('Gokul',25,'IT','USA');
    commit;
    
    insert into employee5 values ('Mohan',25,'ECE','India');
    insert into employee5 values ('Raja',24,'IT','India');
    insert into employee5 values ('Palani',26,'ECE','USA');
    insert into employee5 values ('Sathesh',29,'CSE','CANADA');
    insert into employee5 values ('Ahamed',28,'ECE','UK');
    insert into employee5 values ('Gokul',29,'EEE','USA');
    commit;
    

    更新数据之前...

    SQL> select * from student;
    
    NAME                        AGE DEP HOMECOUNTR
    -------------------- ---------- --- ----------
    Mohan                        25 EEE India
    Raja                         27 EEE India
    Ahamed                       26 ECE UK
    Gokul                        25 IT  USA
    
    SQL> select * from employee5;
    
    NAME                        EXP QUA HOMECOUNTR
    -------------------- ---------- --- ----------
    Mohan                        25 ECE India
    Raja                         24 IT  India
    Palani                       26 ECE USA
    Sathesh                      29 CSE CANADA
    Ahamed                       28 ECE UK
    Gokul                        29 EEE USA
    

    更新声明和结果

      1  update student s set s.age =
      2     ( select e.exp
      3          from employee5 e
      4          where e.name = s.name
      5            and e.homecountry = s.homecountry
      6*    )
    SQL> /
    
    4 rows updated.
    
    SQL> select * from student;
    
    NAME                        AGE DEP HOMECOUNTR
    -------------------- ---------- --- ----------
    Mohan                        25 EEE India
    Raja                         24 EEE India
    Ahamed                       28 ECE UK
    Gokul                        29 IT  USA
    
    SQL> commit;
    
    Commit complete.
    

    【讨论】:

    • 谢谢..Rajesh,我在您的查询中遇到的问题是我不想更新学生表中的所有部门值。实际上,我需要使用您在 select 语句中使用的相同 WHERE 条件。
    • 你能用一些数据解释一下吗?用语言理解需求有点困难。
    • 我不确定您还在寻找什么 - @Rajesh 的查询符合您的要求,即 update Student table's department column with Employee table's qualification column values under the where condition Student.uniquename = Employee.uniquename and Student.Homecountry = Employee.Homecountry. 如果您不想更新所有部门值 - 只需添加所选部门where 条件的值 - update students set (..) where <your-condition>
    • 这里我的 where 条件包括 2 个表(Student.Homecountry = Employee.Homecountry)。但在更新语句中,我们不能使用多个表表。我想更新学生表中的 100 条记录。当我运行上述查询时,它说子查询返回多行。我应该使用游标更新一个表列中的多个记录与另一个表的列。我是甲骨文的新手,如果我错了,请纠正我
    • “子查询返回多行”发生在给定学生的情况下,您从员工表中找到多个记录。如果您能花时间发布您的数据和表格详细信息,那将非常有帮助。
    【解决方案2】:
    update student s 
       set s.age = (select e.exp 
                      from employee5 e 
                      where e.name = s.name 
                        and e.homecountry = s.homecountry  
                        and rownum < 2
                   )
    where s.age in (select age from employee5)
    

    它不会显示消息子查询返回多条记录

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2021-10-16
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 2019-04-10
      相关资源
      最近更新 更多